我正在尝试使用 SwiftUI
进行以下布局。
表单标签的trailing
alignment
与父 View 的中心对齐。同样,TextFields
的前导对齐方式也与父 View 的中心对齐。
我有以下代码-
HStack {
Spacer()
VStack {
HStack {
Spacer()
Text("Name")
TextField("", text: .constant(""))
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(maxWidth: 100)
}
HStack {
Spacer()
Text("Adress")
TextField("", text: .constant(""))
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(maxWidth: 100)
}
HStack {
Spacer()
Text("Short Description")
TextField("", text: .constant(""))
.textFieldStyle(RoundedBorderTextFieldStyle())
.frame(maxWidth: 100)
}
}
.layoutPriority(1)
Spacer()
}
结果
最佳答案
我现在没有Xcode
但是为什么你不能把所有的文本都放到一个Vstack
和TextField
里面一个单独的 Vstack
?像这样的东西
HStack {
HStack {
VStack(alignment: .trailing) {
Spacer()
Text("Name")
Text("Adress")
Text("Short Description")
}
VStack{
BorderedTextField(placeholder: "", text: .constant(""))
.frame(maxWidth: 100)
BorderedTextField(placeholder: "", text: .constant(""))
.frame(maxWidth: 100)
BorderedTextField(placeholder: "", text: .constant(""))
.frame(maxWidth: 100)
}
}
.layoutPriority(1)
Spacer()
}
另一种可能性是创建自定义对齐方式,对此有很好的讨论 here from WWDC
关于swift - 如何将 View 的尾部对齐方式与其父级的垂直轴对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57565602/