gpt4 book ai didi

swiftui - VStack 内容居中而不是左对齐

转载 作者:行者123 更新时间:2023-12-02 18:27:19 29 4
gpt4 key购买 nike

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

我得到的:

enter image description here

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

谢谢你的帮助

最佳答案

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

关于swiftui - VStack 内容居中而不是左对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69967491/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com