Toggle

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 calling Toggle or used inside a closure, as shown in the example below.
  • isOn: is a binding Bool 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 = false
var 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:

Toggle when off

This will display the following when the Toggle is on:

Toggle when on

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn SwiftUI on Codecademy