SwiftUI Lists & Forms Section
SwiftUI Lists & Forms: Section
Group rows in lists and inputs in forms using Section with optional headers and footers.
Example: Sections in List
Example
import SwiftUI
struct SectionListDemo: View {
var body: some View {
List {
Section(header: Text("Fruits"), footer: Text("End Fruits")) {
Text("Apple"); Text("Banana")
}
Section(header: Text("Veggies")) { Text("Carrot") }
}
}
}
import SwiftUI
struct ContentView: View { var body: some View { SectionListDemo() } }
import SwiftUI
@main
struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
Example: Sections in Form
Example
import SwiftUI
struct SectionFormDemo: View {
@State private var name = ""
@State private var notifications = true
var body: some View {
Form {
Section(header: Text("Profile"), footer: Text("Shown to others")) {
TextField("Name", text: $name)
}
Section(header: Text("Preferences")) {
Toggle("Notifications", isOn: $notifications)
}
}
}
}
import SwiftUI
struct ContentView: View { var body: some View { SectionFormDemo() } }
import SwiftUI
@main
struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }