TechnologiesSwiftUI

ButtonToggleStyle struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A toggle style that displays as a button with its label as the title.

How it works

ButtonToggleStyle is the toggle style that renders a Toggle as a single button, which appears pressed (filled) when the toggle is on and unpressed (bordered) when it is off. Reach for it when an on/off control reads more naturally as a tappable, state-bearing button than as a switch or checkbox — for example a "Favorite" or "Bold" affordance in a toolbar or inline next to its label. You rarely name the type directly: SwiftUI exposes it through the .button shorthand you pass to the toggleStyle(_:) modifier, and the toggle's own label becomes the button's content.

  1. Apply the style with toggleStyle(.button)

    The toggleStyle(_:) view modifier selects how a Toggle draws itself; passing the .button value installs a ButtonToggleStyle. In the example, .toggleStyle(.button) turns the Toggle(isOn: $isStarred) into a button that toggles isStarred each time it's tapped, swapping between its on and off appearances.

  2. Supply content through the toggle's label

    ButtonToggleStyle reuses the closure you give to Toggle as the button's label, so whatever view describes the toggle becomes the button's face. Here the Label("Favorite", systemImage: "star") provides the star icon and text that fill the button, and they remain the button's content in both the on and off states.

  3. Read the on/off state from the binding

    The style derives its pressed-versus-unpressed look from the Bool binding driving the toggle — when the value is true the button shows its filled, active treatment. The @State private var isStarred backing the isOn: $isStarred binding is what the button reflects, so the appearance changes as that state flips.

  4. Color the selected state with tint(_:)

    A button-styled toggle adopts the surrounding tint for its filled, on-state fill, letting you signal meaning without a custom style. Adding .tint(.yellow) here makes the button render in yellow once isStarred becomes true, matching the "star" semantics of the label.

Try it — Change @State private var isStarred = false to = true and the button starts in its filled, yellow on-state instead of the bordered off-state.

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.

ButtonToggleStyle.swift
struct ButtonToggleStyleDemo: View {
    @State private var isStarred = false

    var body: some View {
        Toggle(isOn: $isStarred) {
            Label("Favorite", systemImage: "star")
        }
        .toggleStyle(.button)
        .tint(.yellow)
        .padding()
    }
}
Live preview
Favorite
Favorite
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →