Type Property 型プロパティ

switch

A toggle style that displays a leading label and a trailing switch. あるトグルスタイル、それは先頭のラベルと後続のスイッチを表示します。

Declaration 宣言

static var `switch`: SwitchToggleStyle { get }
Available when Self is SwitchToggleStyle.

Discussion 議論

Apply this style to a Toggle or to a view hierarchy that contains toggles using the toggleStyle(_:) modifier:


Toggle("Enhance Sound", isOn: $isEnhanced)
    .toggleStyle(.switch)

The style produces a label that describes the purpose of the toggle and a switch that shows the toggle’s state. The user taps or clicks the switch to change the toggle’s state. The default appearance is similar across platforms, although the way you use switches in your user interface varies a little, as described in the respective Human Interface Guidelines sections:

Platform

Appearance

Human Interface Guidelines

iOS, iPadOS

A screenshot of the text On 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, and are aligned with opposite edges of the rectangle.

Switches

macOS

A screenshot of the text On appearing to the left of a toggle switch that’s on. The toggle’s tint color is blue. The toggle and its text are adjacent to each other.

Switches

watchOS

A screenshot of the text On 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, and are aligned with opposite edges of the rectangle.

Toggles and Switches

In iOS, iPadOS, and watchOS, the label and switch fill as much horizontal space as the toggle’s parent offers by aligning the label’s leading edge and the switch’s trailing edge with the containing view’s respective leading and trailing edges. In macOS, the style uses a minimum of horizontal space by aligning the trailing edge of the label with the leading edge of the switch. SwiftUI helps you to manage the spacing and alignment when this style appears in a Form.

SwiftUI uses this style as the default for iOS, iPadOS, and watchOS in most contexts when you don’t set a style, or when you apply the automatic style.

See Also 参照

Getting Built-in Toggle Styles