如何在 swiftUI 中垂直居中表单视图?
How do I vertically center form view in swiftUI?
我正在尝试使用表单作为 username/password 登录屏幕
默认情况下,表单与屏幕顶部对齐,但我希望它在垂直和水平方向居中对齐。
关于如何做到这一点有什么想法吗?
struct LoginView: View {
@State var username: String = ""
@State var password: String = ""
var body: some View {
VStack {
Form {
TextField("Username", text: $username)
TextField("Password", text: $password)
}
}
.frame(width: 400, height: 200, alignment: .center)
}
}
this is how it looks right now
我尝试使用 VStack 和 .frame 修饰符,但我得到的结果看起来像这样,这不是我想要的
This is with .frame and VStack
var body: some View {
VStack(spacing: 0) {
Color(uiColor: .systemGroupedBackground)
List {
TextField("Username", text: $username)
TextField("Password", text: $password)
}
Color(uiColor: .systemGroupedBackground)
}
.ignoresSafeArea()
}
我正在尝试使用表单作为 username/password 登录屏幕 默认情况下,表单与屏幕顶部对齐,但我希望它在垂直和水平方向居中对齐。 关于如何做到这一点有什么想法吗?
struct LoginView: View {
@State var username: String = ""
@State var password: String = ""
var body: some View {
VStack {
Form {
TextField("Username", text: $username)
TextField("Password", text: $password)
}
}
.frame(width: 400, height: 200, alignment: .center)
}
}
this is how it looks right now
我尝试使用 VStack 和 .frame 修饰符,但我得到的结果看起来像这样,这不是我想要的
This is with .frame and VStack
var body: some View {
VStack(spacing: 0) {
Color(uiColor: .systemGroupedBackground)
List {
TextField("Username", text: $username)
TextField("Password", text: $password)
}
Color(uiColor: .systemGroupedBackground)
}
.ignoresSafeArea()
}