Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VStack content is centered instead of aligned left

Tags:

swiftui

vstack

I would like to left-align the content of a VStack instead of centering it, but I don't know how to do it. Here is my code:

struct SortRow: View {
    var sortType: SortType
    @AppStorage(sortKey) var currentSorting: SortType = .winOrLoss
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text(sortType.name)
                .multilineTextAlignment(.leading)
                .font(.system(size: 15.0))
                .foregroundColor(Color(sortType == currentSorting ? UIColor.white : UIColor.systemGray2))
            Text(sortType.detail)
                .multilineTextAlignment(.leading)
                .font(.system(size: 13.0))
                .foregroundColor(Color(sortType == currentSorting ? UIColor.white : UIColor.systemGray))
        }
        .frame(maxWidth: .infinity)
        .padding(12)
        .background(Color(sortType == currentSorting ? UIColor.systemGreen : UIColor.systemBackground))
    }
}

What I get:

enter image description here

Why is there this left and right padding that centers the text content?

Thank you for your help

like image 248
Kelton Avatar asked Nov 27 '25 06:11

Kelton


1 Answers

The frame modifier that you have on the VStack needs an alignment as well:

struct SortRow: View {
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Win or loss")
                .multilineTextAlignment(.leading)
                .font(.system(size: 15.0))
                .foregroundColor(.white)
            Text("Sort by how much money has been won or lost")
                .multilineTextAlignment(.leading)
                .font(.system(size: 13.0))
                .foregroundColor(.white)
        }
        .frame(maxWidth: .infinity, alignment: .leading) //<-- Here
        .padding(12)
        .background(Color(uiColor: UIColor.systemGreen))
    }
}
like image 161
jnpdx Avatar answered Nov 28 '25 22:11

jnpdx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!