Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to center VStack horizontally inside List with SwiftUI?

I have a simple view:

var body: some View {
    NavigationView {
        List(months) { month in
            NavigationLink {
                MonthView(month: month)
            } label: {
                VStack(alignment: .center, spacing: 8, content: {
                    Text("abc")
                    Text("abcdef")
                    Text("a")
                })
            }
            .listRowBackground(
                Color(uiColor: mode.darkUnderlayBackgroundColor)
                    .clipped()
                    .cornerRadius(10)
            )
        }
        .navigationTitle(months.first?.descriptiveYear ?? "")
    }
}

and result is:

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I center it in a whole view?

>Solution :

You just need to set the maxWidth to infinity using the .frame modifier. This will allow it to expand horizontally to take up as much room as it needs

VStack(alignment: .center, spacing: 8) {
    Text("abc")
    Text("abcdef")
    Text("a")
}
.frame(maxWidth: .infinity)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading