Declaration
Initializers
4 SwiftUI initializers back <Toggle>. The props you pass pick one.
<Toggle
isOn: Binding<boolean>
>
children?: ReactNode
</Toggle>nonisolated init(
isOn: Binding<Bool>,
@ContentBuilder label: () -> Label
)isOn*Binding<boolean>A binding to a property that determines whether the toggle is on or off.
childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
label: () -> Label
Also takes the common view props: modifiers, style and testID.
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 Label based labels, you can use the convenience
initializer that takes a title string (or localized string key) and the
name of a system image:
@State private var vibrateOnRing = true
var body: some View {
Toggle(
"Vibrate on Ring",
systemImage: "dot.radiowaves.left.and.right",
isOn: $vibrateOnRing
)
}For 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)
}For cases where adding a subtitle to the label is desired, use a view
builder that creates multiple Text views where the first text represents
the title and the second text represents the subtitle:
@State private var vibrateOnRing = false
var body: some View {
Toggle(isOn: $vibrateOnRing) {
Text("Vibrate on Ring")
Text("Enable vibration when the phone rings")
}
}Styling toggles
Toggles use a default style that varies based on both the platform and
the context. For more information, read about the ToggleStyle.automatic
toggle style.
You can customize the appearance and interaction of toggles by applying
styles using the View.toggleStyle(_:) modifier. You can apply built-in
styles, like ToggleStyle.switch, to either a toggle, or to a view
hierarchy that contains toggles:
VStack {
Toggle("Vibrate on Ring", isOn: $vibrateOnRing)
Toggle("Vibrate on Silent", isOn: $vibrateOnSilent)
}
.toggleStyle(.switch)You can also define custom styles by creating a type that conforms to the
ToggleStyle protocol.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Controls.
- ButtonA control that initiates an action.
- ColorPickerA control used to select a color from the system color picker UI.
- DatePickerA control for selecting an absolute date.
- DisclosureGroupA view that shows or hides another content view, based on the state of a disclosure control.
- MultiDatePickerA control for picking multiple dates.
- OutlineGroupA structure that computes views and disclosure groups on demand from an underlying collection of tree-structured, identified data.
- PasteButtonA system button that reads items from the pasteboard and delivers it to a closure.
- PickerA control for selecting from a set of mutually exclusive values.
- SecureFieldA control into which people securely enter private text.
- SliderA control for selecting a value from a bounded linear range of values.
- StepperA control that performs increment and decrement actions.
- SubscriptionViewA view that subscribes to a publisher with an action.