Skip to content

SwiftUI forms: Toggle

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.


β†’ Get my Swift Handbook

I wrote 21 books to help you become a better developer:

  • HTML Handbook
  • Next.js Pages Router Handbook
  • Alpine.js Handbook
  • HTMX Handbook
  • TypeScript Handbook
  • React Handbook
  • SQL Handbook
  • Git Cheat Sheet
  • Laravel Handbook
  • Express Handbook
  • Swift Handbook
  • Go Handbook
  • PHP Handbook
  • Python Handbook
  • Linux Commands Handbook
  • C Handbook
  • JavaScript Handbook
  • Svelte Handbook
  • CSS Handbook
  • Node.js Handbook
  • Vue Handbook
...download them all now!

Related posts that talk about swift: