使用 Section
可以将 Form
分为多个部分,每个 Section
可以包含一组相关的表单控件。
以下是一个简单的示例代码,其中一个 Form
包含两个 Section
:
struct ContentView: View {
@State private var name = ""
@State private var age = 0
@State private var email = ""
var body: some View {
NavigationView {
Form {
Section(header: Text("Personal Information")) {
TextField("Name", text: $name)
Stepper("Age: \(age)", value: $age, in: 0...100)
}
Section(header: Text("Contact Information")) {
TextField("Email", text: $email)
}
}
.navigationBarTitle("Form")
}
}
}
在这个示例中,第一个 Section
包含一个文本框(用于输入名称),和一个步进器(用于选择年龄)。header
修饰符被用来添加 Personal Information
的标头。第二个 Section
仅包含一个文本框(用于输入电子邮件地址),并使用 Contact Information
作为标头。