TechnologiesSwiftUI

Toggle struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

A control that toggles between on and off states.

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.

  1. Bind the on/off state with the isOn parameter

    The core initializer pairs a label with an isOn binding to a Bool, and the control reads from and writes back to that source of truth. Here Toggle("Wi-Fi", isOn: $isOn) drives the @State private var isOn value, so flipping the switch mutates isOn and any view observing it updates.

  2. 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 a Text label 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.

  3. Pass the binding with the $ projection

    The isOn parameter expects a Binding<Bool>, not a plain value, so you write $isOn to pass the projected binding of the @State property. The $ prefix gives Toggle read-write access, which is what lets a tap change isOn from true to false and back.

  4. Adjust layout and appearance with view modifiers

    Because Toggle is a View, the standard modifier chain applies — here .padding() insets the control from surrounding content. You can further style it with .toggleStyle(...) (for example .switch or .button) to change how the on/off state is presented.

Try it — Change the initial value to @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.

Toggle.swift
struct ToggleDemo: View {
    @State private var isOn = true

    var body: some View {
        Toggle("Wi-Fi", isOn: $isOn)
            .padding()
    }
}
Live preview
Wi-Fi
Wi-Fi
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →