How it works
A Toggle is a control that switches between an on and an off state, the SwiftUI counterpart to a physical switch or checkbox. You bind it to a Bool value, and the control both reflects that value and updates it as the person flips it — keeping your state and the UI in sync without manual event handling. Reach for Toggle whenever a setting is binary: enabling Wi-Fi, turning on notifications, or any preference that is simply on or off.
Bind the on/off state with the isOn parameter
The core initializer pairs a label with an
isOnbinding to aBool, and the control reads from and writes back to that source of truth. HereToggle("Wi-Fi", isOn: $isOn)drives the@State private var isOnvalue, so flipping the switch mutatesisOnand any view observing it updates.Provide a label with the string-convenience initializer
Passing a string literal as the first argument, like
"Wi-Fi", uses the convenience initializer that builds aTextlabel for you, describing what the switch controls. Toggle also offers a trailing-closure form (Toggle(isOn:) { ... }) when you need a richer label built from arbitrary views.Pass the binding with the $ projection
The
isOnparameter expects aBinding<Bool>, not a plain value, so you write$isOnto pass the projected binding of the@Stateproperty. The$prefix gives Toggle read-write access, which is what lets a tap changeisOnfromtruetofalseand back.Adjust layout and appearance with view modifiers
Because
Toggleis aView, the standard modifier chain applies — here.padding()insets the control from surrounding content. You can further style it with.toggleStyle(...)(for example.switchor.button) to change how the on/off state is presented.
@State private var isOn = false and the switch renders in its off position, showing how Toggle mirrors the bound Bool.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct ToggleDemo: View {
@State private var isOn = true
var body: some View {
Toggle("Wi-Fi", isOn: $isOn)
.padding()
}
}