Generic Structure

Toggle

A control that toggles between on and off states.

Declaration

struct Toggle<Label> where Label : View

Overview

You create a toggle by providing an isOn binding and a label. Bind isOn to a Boolean property that determines whether the toggle is on or off. Set the label to a view that visually describes the purpose of switching between toggle states. For example:

@State private var vibrateOnRing = false

var body: some View {
    Toggle(isOn: $vibrateOnRing) {
        Text("Vibrate on Ring")
    }
}

For the common case of text-only labels, you can use the convenience initializer that takes a title string (or localized string key) as its first parameter, instead of a trailing closure:

@State private var vibrateOnRing = true

var body: some View {
    Toggle("Vibrate on Ring", isOn: $vibrateOnRing)
}

Styling Toggles

You can customize the appearance and interaction of toggles by creating styles that conform to the ToggleStyle protocol. To set a specific style for all toggle instances within a view, use the toggleStyle(_:) modifier:

VStack {
    Toggle("Vibrate on Ring", isOn: $vibrateOnRing)
    Toggle("Vibrate on Silent", isOn: $vibrateOnSilent)
}
.toggleStyle(SwitchToggleStyle())

Topics

Creating a Toggle

init(isOn: Binding<Bool>, label: () -> Label)

Creates a toggle that displays a custom label.

init<S>(S, isOn: Binding<Bool>)

Creates a toggle that generates its label from a string.

init(LocalizedStringKey, isOn: Binding<Bool>)

Creates a toggle that generates its label from a localized string key.

init(ToggleStyleConfiguration)

Creates a toggle based on a toggle style configuration.

Styling a Toggle

protocol ToggleStyle

A type that specifies the appearance and interaction of all toggles within a view hierarchy.

struct DefaultToggleStyle

The default toggle style.

struct SwitchToggleStyle

A toggle style that displays a leading label and a trailing switch.

struct CheckboxToggleStyle

A toggle style that displays a checkbox followed by its label.

func toggleStyle<S>(S) -> View

Sets the style for toggles within this view.

Applying Standard Modifiers

View Modifiers

Configure this view and the views it contains by applying standard modifiers.

Relationships

Conforms To

See Also

Value Selectors

struct Picker

A control for selecting from a set of mutually exclusive values.

struct DatePicker

A control for selecting an absolute date.

struct Slider

A control for selecting a value from a bounded linear range of values.

struct Stepper

A control used to perform semantic increment and decrement actions.