<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/toggleStyle(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE11toggleStyleyQrqd__AA06ToggleE0Rd__lF"
  },
  "title" : "toggleStyle(_:)"
}
-->

# toggleStyle(_:)

Sets the style for toggles in a view hierarchy.

```
nonisolated func toggleStyle<S>(_ style: S) -> some View where S : ToggleStyle
```

## Parameters

`style`

The toggle style to set. Use one of the built-in
values, like [`switch`](/documentation/SwiftUI/ToggleStyle/switch) or [`button`](/documentation/SwiftUI/ToggleStyle/button),
or a custom style that you define by creating a type that conforms
to the [`ToggleStyle`](/documentation/SwiftUI/ToggleStyle) protocol.

## Return Value

A view that uses the specified toggle style for itself
and its child views.

## Discussion

Use this modifier on a [`Toggle`](/documentation/SwiftUI/Toggle) instance to set a style that defines
the control’s appearance and behavior. For example, you can choose
the [`switch`](/documentation/SwiftUI/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:

|Platform   |Appearance                                                                                                                                                                                                        |
|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|iOS, iPadOS|![A screenshot of the text Vibrate on Ring appearing to the left of a toggle switch that’s on. The toggle’s tint color is green. The toggle and its text appear in a rounded rectangle.](View-toggleStyle-1-iOS)  |
|macOS      |![A screenshot of the text Vibrate on Ring appearing to the left of a toggle switch that’s on. The toggle’s tint color is blue. The toggle and its text appear on a neutral background.](View-toggleStyle-1-macOS)|

### 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 [`button`](/documentation/SwiftUI/ToggleStyle/button) style to an [`HStack`](/documentation/SwiftUI/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`:

|Platform   |Appearance                                                                                                                                                                                                                                        |
|-----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|iOS, iPadOS|![A screenshot of two buttons arranged horizontally. The first has the image of a flag and is active with a blue tint. The second has an image of a speaker with a line through it and is inactive with a neutral tint.](View-toggleStyle-2-iOS)  |
|macOS      |![A screenshot of two buttons arranged horizontally. The first has the image of a flag and is active with a blue tint. The second has an image of a speaker with a line through it and is inactive with a neutral tint.](View-toggleStyle-2-macOS)|

### Automatic styling

If you don’t set a style, SwiftUI assumes a value of
[`automatic`](/documentation/SwiftUI/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
[`checkbox`](/documentation/SwiftUI/ToggleStyle/checkbox), while in iOS, the default toggle style is a
[`switch`](/documentation/SwiftUI/ToggleStyle/switch):

|Platform   |Appearance                                                                                                                                                                                                                                                                                                                                                                                                             |
|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|iOS, iPadOS|![A screenshot of several horizontally arranged items: two buttons, a vertical divider line, the text Enhance sound, and a switch. The first button has two right facing arrows that cross over in the middle and is active with a blue tint. The second button has one right and one left facing arrow and is inactive with neutral tint. The switch is on and has a green tint.](View-toggleStyle-3-iOS)             |
|macOS      |![A screenshot of several horizontally arranged items: two buttons, a vertical divider line, a checkbox, and the text Enhance sound. The first button has two right facing arrows that cross over in the middle and is active with a blue tint. The second button has one right and one left facing arrow and is inactive with a neutral tint. The check box is checked and has a blue tint.](View-toggleStyle-3-macOS)|

> Note: Like toggle style does for toggles, the ``doc://com.apple.SwiftUI/documentation/SwiftUI/View/labelStyle(_:)``
> modifier sets the style for ``doc://com.apple.SwiftUI/documentation/SwiftUI/Label`` instances in the hierarchy. The
> example above demostrates the compact ``doc://com.apple.SwiftUI/documentation/SwiftUI/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 [`automatic`](/documentation/SwiftUI/ToggleStyle/automatic) style.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)