Declaration
compose().toggleStyle(style: SwiftUI.ToggleStyle)nonisolated func toggleStyle<S>(
_ style: S
) -> some View
where S : ToggleStyleParameters
style*SwiftUI.ToggleStyleThe toggle style to set. Use one of the built-in values, like
ToggleStyle.switchorToggleStyle.button, or a custom style that you define by creating a type that conforms to theToggleStyleprotocol.
Return value
A view that uses the specified toggle style for itself and its child views.
Discussion
Use this modifier on a Toggle instance to set a style that defines
the control's appearance and behavior. For example, you can choose
the ToggleStyle.switch style:
Toggle("Vibrate on Ring", isOn: $vibrateOnRing)
.toggleStyle(.switch)Built-in styles typically have a similar appearance across platforms, tailored to the platform's overall style:
Styling toggles in a hierarchy
You can set a style for all toggle instances within a view hierarchy
by applying the style modifier to a container view. For example, you
can apply the ToggleStyle.button style to an HStack:
HStack {
Toggle(isOn: $isFlagged) {
Label("Flag", systemImage: "flag.fill")
}
Toggle(isOn: $isMuted) {
Label("Mute", systemImage: "speaker.slash.fill")
}
}
.toggleStyle(.button)The example above has the following appearance when isFlagged is
true and isMuted is false:
Automatic styling
If you don't set a style, SwiftUI assumes a value of
ToggleStyle.automatic, which corresponds to a context-specific
default. Specify the automatic style explicitly to override a
container's style and revert to the default:
HStack {
Toggle(isOn: $isShuffling) {
Label("Shuffle", systemImage: "shuffle")
}
Toggle(isOn: $isRepeating) {
Label("Repeat", systemImage: "repeat")
}
Divider()
Toggle("Enhance Sound", isOn: $isEnhanced)
.toggleStyle(.automatic) // Revert to the default style.
}
.toggleStyle(.button) // Use button style for toggles in the stack.
.labelStyle(.iconOnly) // Omit the title from any labels.The style that SwiftUI uses as the default depends on both the platform
and the context. In macOS, the default in most contexts is a
ToggleStyle.checkbox, while in iOS, the default toggle style is a
ToggleStyle.switch:
modifier sets the style for Label instances in the hierarchy. The
example above demostrates the compact LabelStyle.iconOnly style,
which is useful for button toggles in space-constrained contexts.
Always include a descriptive title for better accessibility.
For more information about how SwiftUI chooses a default toggle style,
see the ToggleStyle.automatic style.
See also
More in Styles.
- arrangementViewStyleSets the style for arrangement views within this view.
- backgroundStyleSets the specified style to render backgrounds within the view.
- buttonStyleSets the style for buttons within this view to a button style with a custom appearance and custom interaction behavior.
- controlGroupStyleSets the style for control groups within this view.
- datePickerStyleSets the style for date pickers within this view.
- disclosureGroupStyleSets the style for disclosure groups within this view.
- foregroundStyleSets the primary, secondary, and tertiary levels of the foreground style.
- formStyleSets the style for forms in a view hierarchy.
- gaugeStyleSets the style for gauges within this view.
- groupBoxStyleSets the style for group boxes within this view.
- indexViewStyleSets the style for the index view within the current environment.
- labeledContentStyleSets a style for labeled content.