Showing and hiding form rows
About 2 min
Showing and hiding form rows 관련
SwiftUI by Example
Back to Home
Showing and hiding form rows | SwiftUI by Example
Showing and hiding form rows
Updated for Xcode 15
SwiftUI lets us add and remove items from a form as needed, which is particularly helpful when you want to adjust the list of options that are visible based on previous choices.
For example, this shows a single toggle that prompts the user whether they want to show more advanced options. When that toggle is enabled, a second toggle appears allowing them to enable logging:
struct ContentView: View {
@State private var showingAdvancedOptions = false
@State private var enableLogging = false
var body: some View {
Form {
Section {
Toggle("Show advanced options", isOn: $showingAdvancedOptions.animation())
if showingAdvancedOptions {
Toggle("Enable logging", isOn: $enableLogging)
}
}
}
}
}
Notice how I attached animation()
to the $showingAdvancedOptions
binding, to enable implicit animations for view changes made as a result of any binding change.
Similar solutions…
SwiftUI tips and tricks | SwiftUI by Example
SwiftUI tips and tricks
All SwiftUI property wrappers explained and compared | SwiftUI by Example
All SwiftUI property wrappers explained and compared
How to align form text and controls neatly with LabeledContent | SwiftUI by Example
How to align form text and controls neatly with LabeledContent
How to let users move rows in a list | SwiftUI by Example
How to let users move rows in a list
How to let users delete rows from a list | SwiftUI by Example
How to let users delete rows from a list