我在 SwiftUI
中实现了以下布局:
VStack {
TextField(...)
TextField(...)
Button(...)
}
我试着让我的 VStack 成为父 View 的 50%,所以我添加了以下修饰符:
VStack { ... }
.relativeWidth(0.5)
不是 50% 的 super View ,但我更感兴趣的是实现水平居中。有人知道如何在 SwiftUI 中实现这一点吗?
有一个可用的 alignmentGuide(_:computeValue:)
修饰符,但我正在努力提供正确的输入以满足编译器的要求。
最佳答案
我会使用一些垫片和 HStack ...
struct LoginView: View {
@State var myText: String = ""
var body: some View {
HStack {
Spacer()
VStack {
TextField($myText, placeholder: Text("Email"))
.textFieldStyle(.roundedBorder)
.textContentType(.username)
TextField($myText, placeholder: Text("Password"))
.textFieldStyle(.roundedBorder)
.textContentType(.password)
Button(action: {
// Do your login thing here
}) {
Text("Login")
}
}
Spacer()
}
}
}
关于swift - 使用 SwiftUI 调整宽度/水平对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56469743/