SwiftUI - Toggle



Toggle is a UI control in SwiftUI which allows the user to work with binary states that are true or false, 0 or 1, or on or off. It is commonly used where the user wants to enable or disable the feature in a particular view. It is quite similar to switches.

It is quite popular among developers due to its simplicity, compact size and small storage requirement. Notification settings, blue-tooth settings, Airplane mode, etc are some real-life examples of toggle view.

Creating Toggle in SwiftUI

In SwiftUI, we can create a toggle view easily with the help of a pre-defined Toggle view. It has only one requirement which is the binding value, this binding value represents the current state of the toggle in boolean form. To bind the toggle with the current view we have to pass this binding value in the Toogle() view.

Syntax

Following is the basic toggle syntax −

Toggle(
   _ titleKey: LocalizedStringKey,
   isOn: Binding
)

Parameters

Following parameters are used by this view −

  • titleKey: Represent the motive of the toggle view.

  • isOn: Represent the binding property that determines the state of the toggle view.

We can also use toggle view with the following parameters −

Toggle Description
Toggle(isOn:Binding, label: () -> Lable) It is used to create a toggle with a custom label
Toggle(_:image:isOn:) It is used to create a toggle with an image.
Toggle(_:systemImage:isOn:) It is used to create a toggle with a system image.

Example

The following SwiftUI program is used to create a toggle view.

import SwiftUI

struct ContentView: View {
   @State private var result = false
   var body: some View {
      List{
         
         // Simple toggle
         Toggle("Bluetooth", isOn: $result)
         
         // Toggle with image
         Toggle("Notification", systemImage: "bell", isOn: $result)
      }
   }
}   
#Preview {
   ContentView()
}

Output

Toggle

Styling Toggle in SwiftUI

In SwiftUI, we are allowed to style toggle with the help of the toggleStyle() modifier. It is used to apply pre-defined as well as custom styles to toggle views.

Syntax

Following is the basic toggle syntax −

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

Parameter

It takes only one parameter which is the style of the toggle, it can be pre-defined or custom style. The pre-defined styles supported by the toggle view are as follows −

  • switch: It is used to create a switch style toggle.

  • button: It is used to create a button style toggle.

  • automatic: It is used to create a default system style toggle.

Example

The following SwiftUI program is used to style toggle view.

import SwiftUI

struct ContentView: View {
   @State private var result = false
   var body: some View {
      List{
         Toggle("Bluetooth", isOn: $result).toggleStyle(.button)
         Toggle("Notification", isOn: $result).toggleStyle(.automatic)
         Toggle("Airplane", isOn: $result).toggleStyle(.switch)
      }
   }
}
   
#Preview {
   ContentView()
}

Output

Toggle
Advertisements