
- 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 - Checkbox
A checkbox is a UI component which takes input from the user by providing a box in which the user can select or deselect, check or uncheck an option. We can select single as well as multiple check-boxes at the same time from the given list. It is commonly used in forms, settings, and selecting terms and conditions, permissions, etc.
SwiftUI does not provide an in-built Checkbox view. So, we have to create a custom checkbox which can take values from the user.
Creating Checkbox in SwiftUI
We can create a checkbox in SwiftUI with the help of toggle UI control. It is the easiest and fastest way to create a checkbox. Here, we create a custom style that style toggle as a checkbox. We can also reuse this style multiple times throughout the project. So to create a custom check box, follow the following steps −
Step 1: Open project
Open a new or existing project in XCode.
Step 2: Import SwiftUI
Import SwiftUI in the project.
import SwiftUI
Step 3: Define State Variable
Define a state variable using the @State property to handle the change in the boolean value of the checkbox. If the value is checked, it means true, whereas if the value is false, it means the checkbox is unchecked.
@State private var toggleValue1 = false
Step 4: Create Toggle View
Create a Toggle view that can easily apply custom style, which converts the toggle view into a checkbox.
Toggle("TransGender", isOn: $toggleValue3) .toggleStyle(checkBoxStyle()) .padding(.leading, 69)
Step 5: Custom Style
Define a custom style that conforms to ToggleStyle. It changes the appearance of the Toggle view into a checkbox.
struct checkBoxStyle: ToggleStyle{ func makeBody(configuration: Self.Configuration) -> some View { HStack{ Image(systemName: configuration.isOn ? "checkmark.square" : "square") .resizable() .frame(width:26, height:26) .onTapGesture {configuration.isOn.toggle() } configuration.label } } }
Step 6: Customize Checkbox
We can also style(such as font color, font style, padding, etc) the appearance of the checkbox with the help of pre-defined modifiers like .background(), .foregroundStyle(), .font(), bold(), etc.
Toggle("Female", isOn: $toggleValue2) .toggleStyle(checkBoxStyle()) .padding(.leading, 20) .foregroundStyle(.green)
Example
The following SwiftUI program to create a checkbox.
import SwiftUI struct ContentView: View { @State private var toggleValue1 = false @State private var toggleValue2 = false @State private var toggleValue3 = false var body: some View { VStack { Text("Select your gender:").font(.headline) Toggle("Male", isOn: $toggleValue1) .toggleStyle(checkBoxStyle()) .foregroundStyle(.red) Toggle("Female", isOn: $toggleValue2) .toggleStyle(checkBoxStyle()) .padding(.leading, 20) .foregroundStyle(.green) Toggle("TransGender", isOn: $toggleValue3) .toggleStyle(checkBoxStyle()) .padding(.leading, 69) } } } // Custom Style for CheckBox struct checkBoxStyle: ToggleStyle{ func makeBody(configuration: Self.Configuration) -> some View { HStack{ Image(systemName: configuration.isOn ? "checkmark.square" : "square") .resizable() .frame(width:26, height:26) .onTapGesture {configuration.isOn.toggle() }configuration.label } } } #Preview { ContentView() }
Output
