SwiftUI Toggle: Bindings, Styles, and a Custom ToggleStyle
A hands-on Toggle guide — bind it to state, swap between switch and button styles, build your own ToggleStyle, tint it to your brand, drop it inside Form and List, and gate premium features cleanly. Real Swift 6 code and pitfalls.
A SwiftUI Toggle needs a Bool binding and a label: Toggle("Notifications", isOn: $enabled). Change its look with .toggleStyle(.switch), .toggleStyle(.button), or a custom type conforming to ToggleStyle. Color the switch with .tint(). Inside a Form or List each Toggle becomes a settings row automatically. To gate a premium feature, disable or intercept the toggle unless the user holds the required entitlement.
Variants: Solid, Liquid Glass
Install the free SwiftUI toggle
- Add SwiftKitUI.swift to your app once. It holds the tokens and motion curves.
- Copy SKToggle.swift below into your project. It needs nothing else.
Usage
Toggle("Wi-Fi", isOn: $isOn).toggleStyle(.sk)
Toggle("Wi-Fi", isOn: $isOn).toggleStyle(.sk(.liquidGlass))Full source · 214 lines · MIT
//
// SKToggle.swift
// Swift Kit UI
//
// Requires: SwiftKitUI.swift
//
// A toggle you can *drag*, not just tap. That one detail is most of why the
// system switch feels expensive and a hand-rolled one usually does not — the
// knob tracks your finger, squashes while held, and commits on release using
// gesture velocity rather than a naive midpoint test.
//
// Usage:
// Toggle("Wi-Fi", isOn: $isOn).toggleStyle(.sk)
// Toggle("Wi-Fi", isOn: $isOn).toggleStyle(.sk(.liquidGlass))
//
import SwiftUI
public struct SKToggleStyle: ToggleStyle {
public enum Track: Sendable {
/// Solid fill that crossfades to the accent colour.
case solid
/// iOS 26 Liquid Glass, with an approximation below iOS 26.
case liquidGlass
}
private let track: Track
private let width: CGFloat
private let height: CGFloat
public init(_ track: Track = .solid, width: CGFloat = 52, height: CGFloat = 32) {
self.track = track
self.width = width
self.height = height
}
public func makeBody(configuration: Configuration) -> some View {
SKToggleBody(
configuration: configuration,
track: track,
width: width,
height: height
)
}
}
private struct SKToggleBody: View {
let configuration: ToggleStyleConfiguration
let track: SKToggleStyle.Track
let width: CGFloat
let height: CGFloat
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@Environment(\.isEnabled) private var isEnabled
/// Live finger offset while dragging, in points from the resting position.
@State private var dragOffset: CGFloat = 0
@State private var isPressed = false
private var inset: CGFloat { 3 }
private var knobSize: CGFloat { height - inset * 2 }
private var travel: CGFloat { width - knobSize - inset * 2 }
/// Resting x-offset for the current state, before any drag is applied.
private var restingOffset: CGFloat {
configuration.isOn ? travel : 0
}
/// Clamped so the knob can never leave the track, however far the finger goes.
private var knobOffset: CGFloat {
min(max(restingOffset + dragOffset, 0), travel)
}
/// A held knob widens slightly, exactly like the system switch. Purely
/// cosmetic, and the single cheapest way to make this feel native.
private var knobWidth: CGFloat {
isPressed ? knobSize + 4 : knobSize
}
/// 0…1 across the track. Drives the colour crossfade so the track lights up
/// progressively under the finger instead of snapping at the end.
private var progress: CGFloat {
travel > 0 ? knobOffset / travel : 0
}
var body: some View {
HStack(spacing: 0) {
// The label area is its own tap target. In a settings list the
// whole row should toggle, not just the 52pt switch — a fresh
// agent building a settings screen hit this immediately.
// Kept separate from the switch so the two gestures never race.
HStack {
configuration.label
Spacer(minLength: SK.Space.md)
}
.contentShape(.rect)
.onTapGesture { commit(to: !configuration.isOn) }
switchBody
}
.frame(minHeight: SK.Icon.hitTarget)
}
/// Single commit path for both the row tap and the drag release, so the
/// animation and haptic can never diverge between the two.
private func commit(to newValue: Bool) {
guard isEnabled, newValue != configuration.isOn else { return }
withAnimation(SKMotion.snap.resolved(reduceMotion: reduceMotion)) {
configuration.isOn = newValue
}
SK.Haptic.play(.selection)
}
private var switchBody: some View {
ZStack(alignment: .leading) {
trackView
knobView
.frame(width: knobWidth, height: knobSize)
.offset(x: knobOffset)
.padding(inset)
}
.frame(width: width, height: height)
.opacity(isEnabled ? 1 : 0.45)
.skAnimation(.snap, value: configuration.isOn)
.skAnimation(.micro, value: isPressed)
.gesture(dragGesture, isEnabled: isEnabled)
.accessibilityAddTraits(configuration.isOn ? [.isButton, .isSelected] : .isButton)
}
@ViewBuilder
private var trackView: some View {
let shape = Capsule(style: .continuous)
switch track {
case .solid:
shape
.fill(SK.Palette.fill)
.overlay {
// Crossfaded rather than swapped so a partial drag shows a
// partial colour — the state reads before you commit to it.
shape.fill(SK.Palette.accent).opacity(progress)
}
.overlay {
shape.strokeBorder(SK.Palette.separator.opacity(1 - progress), lineWidth: SK.Stroke.hairline)
}
case .liquidGlass:
Color.clear
.skSurface(.liquidGlass, radius: height / 2)
.overlay {
shape.fill(SK.Palette.accent).opacity(progress * 0.85)
}
}
}
private var knobView: some View {
Capsule(style: .continuous)
.fill(.white)
.shadow(color: .black.opacity(0.18), radius: 3, x: 0, y: 2)
.shadow(color: .black.opacity(0.06), radius: 1, x: 0, y: 0)
}
private var dragGesture: some Gesture {
// minimumDistance 0 so the knob reacts to a plain tap as well, which
// keeps one code path for both interactions.
DragGesture(minimumDistance: 0)
.onChanged { value in
if !isPressed { isPressed = true }
dragOffset = value.translation.width
}
.onEnded { value in
isPressed = false
// Project where the knob would land if the finger kept its
// current velocity. Using predicted rather than final position
// is what makes a fast flick commit even when it is short.
let projected = restingOffset + value.predictedEndTranslation.width
let shouldBeOn = projected > travel / 2
dragOffset = 0
commit(to: shouldBeOn)
}
}
}
extension ToggleStyle where Self == SKToggleStyle {
/// `Toggle(...).toggleStyle(.sk)`
public static var sk: SKToggleStyle { SKToggleStyle() }
/// `Toggle(...).toggleStyle(.sk(.liquidGlass))`
public static func sk(
_ track: SKToggleStyle.Track,
width: CGFloat = 52,
height: CGFloat = 32
) -> SKToggleStyle {
SKToggleStyle(track, width: width, height: height)
}
}
#Preview("SKToggle") {
@Previewable @State var a = true
@Previewable @State var b = false
@Previewable @State var c = true
VStack(spacing: SK.Space.xl) {
Toggle("Solid, on", isOn: $a).toggleStyle(.sk)
Toggle("Solid, off", isOn: $b).toggleStyle(.sk)
Toggle("Liquid Glass", isOn: $c).toggleStyle(.sk(.liquidGlass))
Toggle("Disabled", isOn: .constant(true)).toggleStyle(.sk).disabled(true)
}
.font(SK.Type_.body)
.foregroundStyle(SK.Palette.label)
.padding(SK.Space.xl)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(SK.Palette.surfaceSunken)
}
The binding is the whole point
A Toggle is a two-way control, so it always takes a Binding<Bool> plus a label. The simplest source is an @State property you pass with the $ prefix. Because it's a binding, the Toggle both reads the current value and writes the new one when the user flips it — you never manually respond to a tap. If you need to react to changes (persist a setting, fire analytics), attach .onChange(of:) to the binding's value rather than trying to intercept the toggle itself. For settings that must survive relaunches, back the state with @AppStorage so the value persists to UserDefaults automatically.
struct NotificationSetting: View {
@AppStorage("pushEnabled") private var pushEnabled = true
var body: some View {
Toggle("Push Notifications", isOn: $pushEnabled)
.onChange(of: pushEnabled) { _, newValue in
NotificationManager.shared.setEnabled(newValue)
}
}
}Built-in styles and tint
Toggle ships two built-in styles. The default on iOS is .switch, the familiar sliding capsule. .button turns the toggle into a bordered button that shows a selected state — handy in toolbars or filter bars where a switch would look out of place. Apply either with .toggleStyle. To recolor the switch's on state, use .tint(); the older .toggleStyle(SwitchToggleStyle(tint:)) initializer is deprecated, so prefer the standalone .tint modifier. Tint respects your accent color by default, which is why a single accent applied at the app root keeps every switch on-brand without per-toggle overrides.
- .toggleStyle(.switch) — default sliding switch
- .toggleStyle(.button) — bordered selectable button, great in toolbars
- .tint(.green) — recolors the on state; replaces deprecated SwitchToggleStyle(tint:)
- A label with an SF Symbol works well with .button style
struct StyleShowcase: View {
@State private var wifi = true
@State private var starred = false
var body: some View {
VStack(spacing: 24) {
Toggle("Wi-Fi", isOn: $wifi)
.toggleStyle(.switch)
.tint(.green)
Toggle(isOn: $starred) {
Label("Star", systemImage: "star.fill")
}
.toggleStyle(.button)
}
.padding()
}
}Building a custom ToggleStyle
When neither built-in style fits — say you want a pill with a checkmark, or a card that highlights when on — conform a struct to ToggleStyle. Its makeBody(configuration:) receives a Configuration whose isOn Bool tells you the state and whose label carries your original label view. Crucially, you must toggle configuration.isOn.wrappedValue yourself in the tap handler, because your custom body replaces the default interaction. Wrap the flip in withAnimation for a smooth transition. Once written, apply it with .toggleStyle(MyStyle()) anywhere, and it composes with tint and other modifiers just like the built-ins.
struct CheckPillToggleStyle: ToggleStyle {
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.label
Spacer()
Image(systemName: configuration.isOn ? "checkmark.circle.fill" : "circle")
.foregroundStyle(configuration.isOn ? .accentColor : .secondary)
}
.contentShape(Rectangle())
.onTapGesture {
withAnimation(.snappy) { configuration.isOn.toggle() }
}
}
}
// Usage:
// Toggle("Sync over cellular", isOn: $cellular)
// .toggleStyle(CheckPillToggleStyle())Toggles inside Form and List
Drop a Toggle into a Form or List and SwiftUI renders it as a native settings row — label on the left, control trailing-aligned, with correct insets and separators. This is how you build a Settings screen with almost no layout code. Group related switches under a Section with a header and footer to explain what each does. A subtle pitfall: if a toggle should enable or disable dependent rows, don't hide them abruptly — use .disabled(!parentToggle) so the rows stay visible but dimmed, which is less jarring and keeps the layout stable as the user flips the parent.
struct SettingsForm: View {
@AppStorage("notifications") private var notifications = true
@AppStorage("sound") private var sound = true
var body: some View {
Form {
Section("Alerts") {
Toggle("Notifications", isOn: $notifications)
Toggle("Play Sound", isOn: $sound)
.disabled(!notifications)
}
}
}
}Ship your SwiftUI app in 5 emails
A free 5-part course on the parts that actually stall launches: paywall, auth, onboarding, App Store review, and pricing. No fluff, unsubscribe anytime.
A settings screen you don't have to wire up
The Swift Kit's settings module ships toggles backed by @AppStorage and pro switches gated behind a RevenueCat entitlement — persistence and paywall interception already handled.
Rather have it done for you? I set up and ship your app from $499. See how
How to build a custom ToggleStyle
A custom style lets you replace the switch entirely while keeping Toggle's binding semantics.
- 1
Conform to ToggleStyle
Create a struct that implements makeBody(configuration:).
struct CardToggleStyle: ToggleStyle { func makeBody(configuration: Configuration) -> some View { /* ... */ } } - 2
Read the state
configuration.isOn is a Binding<Bool>; configuration.label is your original label.
let on = configuration.isOn.wrappedValue - 3
Flip it on tap
Your body must mutate configuration.isOn itself, ideally inside withAnimation.
.onTapGesture { withAnimation { configuration.isOn.toggle() } } - 4
Apply the style
Attach it to any Toggle; it composes with tint and other modifiers.
Toggle("Dark Mode", isOn: $dark).toggleStyle(CardToggleStyle())
Frequently Asked Questions
Why does my custom SwiftUI ToggleStyle not respond to taps even though makeBody renders correctly?
How do I change the on-state color of a SwiftUI switch Toggle without using the deprecated SwitchToggleStyle(tint:)?
Can a SwiftUI Toggle drive a value that isn't a simple @State Bool, like an entitlement-gated setting?
Why do my dependent SwiftUI Form rows jump around when a parent Toggle is switched off?
What's the difference between .toggleStyle(.switch) and .toggleStyle(.button) in SwiftUI?
Keep exploring
Ship settings and paywalls the first day
The Swift Kit is a $99 one-time SwiftUI boilerplate with a design system, RevenueCat paywalls, and a settings screen where toggles persist and gate premium features out of the box.
Get The Swift Kit — $99One-time purchase · Lifetime updates · 14-day refund