The SwiftUI framework comes with a built-in checkbox, nevertheless it’s solely obtainable on the macOS platform. If you wish to use checkboxes on iOS, it’s important to construct your individual part. Beforehand, we’ve coated ToggleStyle
to create customized toggles. By making use of the identical method, it’s not laborious to construct a checkbox to your iOS initiatives.
Utilizing Checkbox on macOS
Let’s first have a look how you employ checkbox on macOS. To create a checkbox, you create a Toggle
view and alter it to the .checkbox
model. Right here is an instance:
@State non-public var isChecked = false
var physique: some View {
VStack {
Toggle(isOn: $isChecked) {
Textual content(“Purchase a pack of Airtags”)
}
.toggleStyle(.checkbox)
}
.padding()
}
}
struct ContentView: View {
@State non-public var isChecked = false
var physique: some View { VStack { Toggle(isOn: $isChecked) { Textual content(“Purchase a pack of Airtags”) } .toggleStyle(.checkbox) } .padding() } } |
Should you run the code on macOS, it renders a typical checkbox.

Creating Checkbox Toggle Fashion on iOS
The .checkbox
model doesn’t include the iOS SDK. That stated, it’s fairly simple to create this kind of toggle model utilizing the ToggleStyle
protocol. If you wish to know work with ToggleStyle
, please take a look at our earlier tutorial.
On iOS, we are able to create a brand new model named CheckboxToggleStyle
which adopts the ToggleStyle
protocol and implement the required technique:
RoundedRectangle(cornerRadius: 5.0)
.stroke(lineWidth: 2)
.body(width: 25, top: 25)
.cornerRadius(5.0)
.overlay {
Picture(systemName: configuration.isOn ? “checkmark” : “”)
}
.onTapGesture {
withAnimation(.spring()) {
configuration.isOn.toggle()
}
}
configuration.label
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
struct CheckboxToggleStyle: ToggleStyle { func makeBody(configuration: Configuration) –> some View { HStack {
RoundedRectangle(cornerRadius: 5.0) .stroke(lineWidth: 2) .body(width: 25, top: 25) .cornerRadius(5.0) .overlay { Picture(systemName: configuration.isOn ? “checkmark” : “”) } .onTapGesture { withAnimation(.spring()) { configuration.isOn.toggle() } }
configuration.label
} } } |
Within the makebody
technique, we used a RoundedRectangle
view and overlay it with a “checkmark” picture when the toggle is enabled.

To make use of this toggle model, you merely connect the toggleStyle
modifier and go an occasion of CheckboxToggleStyle
like this:
Toggle(isOn: $isChecked) { Textual content(“Purchase a pack of Airtags”) } .toggleStyle(CheckboxToggleStyle()) |
If you wish to use the dot-syntax, you’ll be able to lengthen ToggleStyle
and declare a corresponding static variable like this:
static var checkmark: CheckboxToggleStyle { CheckboxToggleStyle() }
}
extension ToggleStyle the place Self == CheckboxToggleStyle {
static var checkmark: CheckboxToggleStyle { CheckboxToggleStyle() } } |
Now you’ll be able to apply the model utilizing the dot syntax.
Toggle(isOn: $isChecked) { Textual content(“Purchase a pack of Airtags”) } .toggleStyle(.checkmark) |