Search code examples
swiftuiwatchkitwatchos

SwiftUI: Center button text in List


Is it possible to center button text, if the button is located a in List?

struct HomeView: View {
    var body: some View {
        List {
            ForEach(["1", "2"], id: \.self) {
                Text("\($0)…").frame(maxWidth: .infinity, alignment: .center)
            }

            Button("Action button") {}
        }
    }
}

Result:

screenshot of the result - button has incorrect (left) alignment

PS:

I tried this: .frame(maxWidth: .infinity, alignment: .center) But it doesn't work


Solution

  • Found solution:

    Button{} 
    label: {
        Text("Action").frame(maxWidth: .infinity, alignment: .center)
    }