TechnologiesSwiftUI

DefaultToggleStyle struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

The default toggle style.

How it works

DefaultToggleStyle is the toggle style SwiftUI applies automatically when you don't specify one of your own. It resolves at runtime to the style that's idiomatic for the current context and platform — typically a switch in most layouts, and a checkbox inside forms and other containers where that reads more naturally. Reach for it when you want a Toggle to match the conventional look for where it sits, or when you need to return to that conventional appearance after a custom style was applied higher in the view hierarchy.

  1. Conform to ToggleStyle through the default

    DefaultToggleStyle is a concrete type conforming to the ToggleStyle protocol, so it can stand in anywhere a toggle style is expected. You rarely name its members directly; instead it produces the standard control body for each Toggle it governs, such as Toggle("Wi-Fi", isOn: $wifiOn).

  2. Apply it with toggleStyle(_:)

    The toggleStyle(_:) modifier sets the style for every Toggle in the subtree it's attached to. Here .toggleStyle(DefaultToggleStyle()) is placed on the Form, so both Toggle("Wi-Fi", isOn: $wifiOn) and Toggle("Bluetooth", isOn: $bluetoothOn) adopt the context-appropriate appearance.

  3. Construct it with the no-argument initializer

    DefaultToggleStyle has a single parameterless initializer — there are no options to configure, because the point of this style is to defer the decision to SwiftUI. Writing DefaultToggleStyle() asks the framework to pick the right control for the surrounding context rather than pinning a fixed look.

  4. Let context decide the rendered control

    Because the resolved appearance depends on where the Toggle lives, the same DefaultToggleStyle() can render differently. Inside this Form, the toggles bound to $wifiOn and $bluetoothOn take on the form-appropriate presentation for the platform, whereas the identical declaration outside a form may render as a plain switch.

  5. Use it to override an inherited style

    Applying .toggleStyle(DefaultToggleStyle()) also restores standard behavior when an ancestor view set a different style. Attaching it at the Form re-establishes the default for the toggles within, overriding any toggle style inherited from further up the hierarchy.

Try it — Change .toggleStyle(DefaultToggleStyle()) to .toggleStyle(.switch) and compare how the Wi-Fi and Bluetooth rows render to see what the default resolves to in a form.

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.

DefaultToggleStyle.swift
struct DefaultToggleStyleDemo: View {
    @State private var wifiOn = true
    @State private var bluetoothOn = false

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