SwiftUI: conditionally show items in the view

One common thing to do in a form is to have a toggle and when that toggle is enabled, show a bunch of additional options.

You can see this all the time in the Settings app, for example when you enable WiFi.

How can you do that?

First crea…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

One common thing to do in a form is to have a toggle and when that toggle is enabled, show a bunch of additional options.

You can see this all the time in the Settings app, for example when you enable WiFi.

How can you do that?

First create a Form view with a Toggle control:

struct ContentView: View {
    @State private var enabled = false
    
    var body: some View {
        Form {
            Toggle("Enable?", isOn: $enabled)
        }
    }
}

Then add this block after the Toggle view:

if enabled {
    Section {
        Text("This appears only if enabled")
    }
}

like this:

struct ContentView: View {
    @State private var enabled = false
    
    var body: some View {
        Form {
            Toggle("Enable?", isOn: $enabled)
            
            if enabled {
                Section {
                    Text("This appears only if enabled")
                }
            }
        }
    }
}

Now with the toggle disabled, the Text view will not be visible:

But when you enable the toggle, it appears:


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-10-01T05:00:00+00:00) SwiftUI: conditionally show items in the view. Retrieved from https://www.scien.cx/2021/10/01/swiftui-conditionally-show-items-in-the-view/

MLA
" » SwiftUI: conditionally show items in the view." flaviocopes.com | Sciencx - Friday October 1, 2021, https://www.scien.cx/2021/10/01/swiftui-conditionally-show-items-in-the-view/
HARVARD
flaviocopes.com | Sciencx Friday October 1, 2021 » SwiftUI: conditionally show items in the view., viewed ,<https://www.scien.cx/2021/10/01/swiftui-conditionally-show-items-in-the-view/>
VANCOUVER
flaviocopes.com | Sciencx - » SwiftUI: conditionally show items in the view. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/01/swiftui-conditionally-show-items-in-the-view/
CHICAGO
" » SwiftUI: conditionally show items in the view." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/10/01/swiftui-conditionally-show-items-in-the-view/
IEEE
" » SwiftUI: conditionally show items in the view." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/10/01/swiftui-conditionally-show-items-in-the-view/. [Accessed: ]
rf:citation
» SwiftUI: conditionally show items in the view | flaviocopes.com | Sciencx | https://www.scien.cx/2021/10/01/swiftui-conditionally-show-items-in-the-view/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.