On/Off with Toggle
Bind a Toggle to a boolean setting.
On/Off with Toggle is a free SwiftUI Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Meet the Toggle
A Toggle is SwiftUI's on/off switch. It is perfect for settings like notifications, dark mode, or any yes-or-no choice. 🔘
Toggle Needs a Boolean
Every Toggle reflects a Bool value. When the switch is on the value is true; when it is off the value is false. They stay in sync.
Store It in @State
Hold the on/off value in a @State property so the view can read and update it as the user flips the switch.
@State private var isOn = falseBind with the Dollar Sign
Connect the Toggle to your state using the dollar sign. That creates a two-way binding so flips update the value automatically.
Toggle("Notifications", isOn: $isOn)Give It a Clear Label
The first argument is the label users read next to the switch. Keep it short and describe exactly what the toggle controls.
Toggle("Dark Mode", isOn: $isDark)React to the Value
Read the boolean anywhere in your body to react to the switch. Here the text changes based on whether the toggle is on.
Text(isOn ? "On" : "Off")Put It in a Form
Toggles look most at home inside a Form, where they automatically gain the familiar iOS settings-row styling.
Form {
Toggle("Wi-Fi", isOn: $wifiOn)
}A Richer Label View
Need an icon too? Use the closure form so the label can be any view, like a Label with an SF Symbol.
Toggle(isOn: $isOn) {
Label("Sound", systemImage: "speaker")
}Style the Switch
Change the look with toggleStyle. The button style turns it into a tappable button instead of the default switch.
Toggle("Star", isOn: $isOn)
.toggleStyle(.button)Tint the Track
Set the on-color of the switch with the tint modifier, so it matches your app's accent and feels branded.
Toggle("On", isOn: $isOn)
.tint(.green)The Full View
Here it all comes together: one boolean, one Toggle bound to it, and a Text that responds to every flip.
struct SettingsView: View {
@State private var isOn = false
var body: some View {
Toggle("Alerts", isOn: $isOn)
Text(isOn ? "On" : "Off")
}
}Quick Check
How do you connect a Toggle to a boolean state value?
Recap
A Toggle binds to a Bool with the dollar sign, flips it on tap, and lets you react, style, and tint the switch. 🔁
Frequently asked questions
Is the “On/Off with Toggle” lesson free?
Yes — the full text of “On/Off with Toggle” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “On/Off with Toggle”?
Bind a Toggle to a boolean setting. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “On/Off with Toggle” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.