VStack内容居中而不是左对齐

Kel*_*ton 8 swiftui vstack

我想将 a 的内容左对齐VStack而不是居中,但我不知道该怎么做。这是我的代码:

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))
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到什么:

在此输入图像描述

为什么会有这样的左右填充来使文本内容居中?

感谢您的帮助

jn_*_*pdx 21

frame您拥有的修饰符也VStack需要对齐:

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))
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @whitneyland - 一个涉及 vstack 内的对齐,而另一个则涉及 vstack 本身的对齐。例如,您的内容可以在左对齐的 vstack 内居中对齐,并且您的内容可以在中心对齐的 vstack 内左对齐。 (2认同)