[英]How do I vertically center my content on my swiftui app?
I'm a beginner here, and just trying to put together a simple form and button (right now they don't do anything).我是这里的初学者,只是想把一个简单的表单和按钮放在一起(现在他们什么都不做)。 For some reason I can't seem to move the form -- which is just going to be one text input by the way, so open to using a different method other than form -- to be centered vertically.
出于某种原因,我似乎无法移动表单 - 顺便说一下,这只是一个文本输入,因此可以使用表单以外的其他方法 - 垂直居中。 Can anyone help me diagnose the problem?
谁能帮我诊断问题? Here is my code:
这是我的代码:
import SwiftUI
struct CreateTopic: View {
var body: some View {
VStack {
Text("Create Topic")
.font(.largeTitle)
Spacer() // <-- Add a Spacer view above the Form view
Form {
TextField("Name", text: $name)
// TextField("Email", text: $email)
// TextField("Phone", text: $phone)
}
.frame(height: 100)
Spacer() // <-- Add a Spacer view below the Form view
NavigationView {
StandardButton(text: "Test", action: {})
}
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}
@State private var name = ""
// @State private var email = ""
// @State private var phone = ""
}
struct CreateTopic_Previews: PreviewProvider {
static var previews: some View {
CreateTopic()
}
}
I tried adding that Spacer() above the Form, which didn't really do anything.我尝试在 Form 上方添加 Spacer() ,但实际上并没有做任何事情。 It did however move the button.
但是它确实移动了按钮。
Should I not be using a Form if I am only looking to use one simple text input?如果我只想使用一个简单的文本输入,是否应该不使用表单?
There are multiple misunderstandings here.这里有多种误解。 We can quickly solve them.
我们可以快速解决它们。
Form
is a scrollable form. Form
是一个可滚动的表单。 It is typically meant to encompass the entire screen.它通常意味着包含整个屏幕。 And if you use a form, you will not be able to center its content.
如果您使用表单,您将无法将其内容居中。 Forms always lay out their subviews top-to-bottom.
Forms 总是从上到下布置他们的子视图。 You could center a form within a larger view, but I wouldn't.
您可以在更大的视图中将表单居中,但我不会。 I ran your code and you can scroll the form within the 100pt-tall rectangle.
我运行了您的代码,您可以在 100pt 高的矩形内滚动表单。 It's just weird.
这很奇怪。
NavigationView
is meant to wrap the entire screen, not just a single button. NavigationView
旨在包裹整个屏幕,而不仅仅是一个按钮。 It does things like add a navigation bar, display a title, and support buttons in the corners of the screen.它可以添加导航栏、显示标题以及在屏幕角落支持按钮。 It also supports the standard in-and-out screen transitions across all of iOS. Think of the Messages app and what happens when you tap into a conversation.
它还支持跨所有 iOS 的标准进出屏幕转换。想想消息应用程序以及当您点击对话时会发生什么。
NavigationView
does that stuff. NavigationView
做那些事情。
With these two clarifications in mind, I would rewrite your view one of two ways.考虑到这两个澄清,我将以两种方式之一重写您的观点。
Option 1, using a Form选项 1,使用表格
struct CreateTopic: View {
var body: some View {
NavigationView {
Form {
Section {
TextField("Name", text: $name)
.textContentType(.name)
TextField("Email", text: $email)
.textContentType(.emailAddress)
TextField("Phone", text: $phone)
.textContentType(.telephoneNumber)
.keyboardType(.phonePad)
}
Section {
Button("Create") {
// Create the topic
}
}
}
.navigationTitle("Create Topic")
}
}
@State private var name = ""
@State private var email = ""
@State private var phone = ""
}
Option 2, without a Form, centering your text fields like you wanted选项 2,没有表单,按照您想要的方式将文本字段居中
struct CreateTopic: View {
var body: some View {
NavigationView {
VStack {
TextField("Name", text: $name)
.textContentType(.name)
TextField("Email", text: $email)
.textContentType(.emailAddress)
TextField("Phone", text: $phone)
.textContentType(.telephoneNumber)
.keyboardType(.phonePad)
Button("Create") {
// Create the topic
}
}
.textFieldStyle(.roundedBorder)
.padding()
.frame(maxHeight: .infinity)
.background(Color(.systemGroupedBackground))
.navigationTitle("Create Topic")
}
}
@State private var name = ""
@State private var email = ""
@State private var phone = ""
}
Form
fits the style of iOS more, but the second option looks fine and could be what you wanted. Form
更符合 iOS 的风格,但第二个选项看起来不错,可能是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.