Skip to content

SwiftUI forms: Toggle

New Course Coming Soon:

Get Really Good at Git

Another common form control is Toggle.

You can see it widely used in the Settings app.

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

It works similarly to a TextField view, except instead of a String value passed with the text parameter, we pass a Bool value to isOn.

If you set it to true the toggle is enabled, if you set it to false it’s disabled:

Of course the user can enable or disable it by tapping on the control, and that automatically updates the variable passed to isOn.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my Swift Handbook

Here is how can I help you: