Toggle
Published Jan 17, 2023Updated Jan 17, 2023
Contribute to Docs
A Toggle
is a View that chooses between “on or off” options based on a Bool
type value.
Syntax
Toggle("Title String", isOn: Binding<Bool>)
The following parameters can be applied to a Toggle
view:
"Title String"
is an optional parameter. Based on the use-case, this can be applied when callingToggle
or used inside a closure, as shown in the example below.isOn:
is a bindingBool
type parameter that determines the state of the toggle (on or off).
Example
The following example displays a Toggle
that turns a “Wi-Fi” switch on and off by passing the isWifiOn
variable to the isOn:
property. The Label
will be inside a trailing closure.
@State var isWifiOn = falsevar body: some View {Toggle(isOn: $isWifiOn) {Label("Wi-Fi", systemImage: "wifi")}.padding()}
The .padding()
view modifier adds some space to the attached the view. In this way, the toggle and its label are not squished to the end of the screen.
This will display the following when the Toggle
is off:
This will display the following when the Toggle
is on:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.