
- SwiftUI - Home
- SwiftUI - Overview
- SwiftUI vs UIkit
- SwiftUI Environment
- SwiftUI - Environment Setup
- SwiftUI - Basic Components
- SwiftUI - Building First Application
- SwiftUI Views
- SwiftUI - Views
- SwiftUI - Customize Text View
- SwiftUI - Custom Image View
- SwiftUI - Stacks
- SwiftUI Drawing Shapes
- SwiftUI - Shapes
- SwiftUI - Drawing line
- SwiftUI - Drawing Rectangle
- SwiftUI - Drawing Rounded Rectangle
- SwiftUI - Drawing Triangle
- SwiftUI - Drawing Circle
- SwiftUI - Drawing Star
- SwiftUI - Drawing Polygon
- SwiftUI - Drawing Pie chart
- SwiftUI - Using built-in shapes
- SwiftUI - Text
- SwiftUI - Text View
- SwiftUI - Text Input and Output
- SwiftUI - Color
- SwiftUI - Color
- SwiftUI - Colorpicker
- SwiftUI - Gradients
- SwiftUI - Adjust Color
- SwiftUI - Effects
- SwiftUI - Effects
- SwiftUI - Blend Effect
- SwiftUI - BLur Effect
- SwiftUI - Shadow Effect
- SwiftUI - Hover Effect
- SwiftUI - Animations
- SwiftUI - Animations
- SwiftUI - Creating Animations
- SwiftUI - Creating an Explicit Animation
- SwiftUI - Multiple Animations
- SwiftUI - Transitions
- SwiftUI - Asymmetric Transition
- SwiftUI - Custom Transition
- SwiftUI - Image
- SwiftUI - Images
- SwiftUI - Image as Background
- SwiftUI - Rotating Image
- SwiftUI - Media
- SwiftUI - View Layout
- SwiftUI - View Layout
- SwiftUI - View Size
- SwiftUI - View Spacing
- SwiftUI - View Padding
- SwiftUI - UI Controls
- SwiftUI - UI Controls
- SwiftUI - Button
- SwiftUI - CheckBox
- SwiftUI - Menubar
- SwiftUI - Toolbar
- SwiftUI - Search Bar
- SwiftUI - TextField
- SwiftUI - Slider
- SwiftUI - Toggle
- SwiftUI - Pickers
- SwiftUI - Menus
- SwiftUI - List & Tables
- SwiftUI - Lists
- SwiftUI - Static List
- SwiftUI - Dynamic List
- SwiftUI - Customize List
- SwiftUI - Tables
- SwiftUI - Forms
- SwiftUI - Forms
- SwiftUI - Breaking Forms in Sections
- SwiftUI - Event Handling
- SwiftUI - Event Handling
- SwiftUI - Gesture
- SwiftUI - Clipboard
- SwiftUI - Drag and Drop
- SwiftUI - Focus
- SwiftUI - Alert
- SwiftUI - Miscellaneous
- SwiftUI - Containers
- SwiftUI - Navigation
- SwiftUI - Notifications
- SwiftUI - Cross-Platform UI
- SwiftUI - Data
- SwiftUI - Accessibility
- SwiftUI - Framework Integration
- SwiftUI - Framework Integration
- SwiftUI - Interfacing with UIKit
- SwiftUI - Creating macOS App
- SwiftUI Useful Resources
- SwiftUI - Useful Resources
- SwiftUI - Discussion
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 |
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

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
