【问题标题】:VStack content is centered instead of aligned leftVStack 内容居中而不是左对齐
【发布时间】:2021-12-26 06:03:32
【问题描述】:

我想将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))
    }
}

我得到了什么:

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

感谢您的帮助

【问题讨论】:

    标签: swiftui vstack


    【解决方案1】:

    VStack 上的 frame 修饰符也需要对齐:

    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))
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-14
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2013-05-22
      • 2021-10-07
      相关资源
      最近更新 更多