FREE TRIAL: Accelerate your app development career with Hacking with Swift+! >>

Customizing Toggle with ToggleStyle

Paul Hudson    @twostraws   

Updated for Xcode 13.0

SwiftUI gives us the ToggleStyle protocol to customize the way Toggle switches look and work. Any struct that conforms to this protocol must implement a makeBody() method that renders the Toggle however you want it, and you’re giving both the label used for the toggle and an isOn binding that you can flip to adjust the toggle.

Important: When you customize a Toggle switch like this, it’s down to you to flip the on state yourself somehow – SwiftUI will not do it for you.

To demonstrate custom Toggle styles, here’s one that uses a button to flip the on state, then adds a custom label to show that state. Rather than use a moving circle like the standard iOS Toggle, I’ve made this show one of two SF Symbols:

struct CheckToggleStyle: ToggleStyle {
    func makeBody(configuration: Configuration) -> some View {
        Button {
        } label: {
            Label {
            } icon: {
                Image(systemName: configuration.isOn ? "" : "circle")
                    .foregroundColor(configuration.isOn ? .accentColor : .secondary)
                    .accessibility(label: Text(configuration.isOn ? "Checked" : "Unchecked"))

// An example view showing the style in action
struct ContentView: View {
    @State private var isOn = false

    var body: some View {
        Toggle("Switch Me", isOn: $isOn)

Download this as an Xcode project

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Get started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.