Disabling & Enabling Buttons
Control interactivity with the disabled modifier.
Disabling & Enabling Buttons is a free SwiftUI Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, and your progress syncs across the web and the CoddyKit app. The SwiftUI Academy course includes 4 lessons in total.
Sometimes Tapping Should Wait
Not every button should always be tappable. A submit button might wait until a form is valid. SwiftUI handles this with the disabled modifier. 🚦
The disabled Modifier
Add disabled to any control and pass true or false. When true, the button stops responding to taps right away.
Button("Send") { send() }
.disabled(true)Driving It With a Condition
Instead of a fixed value, pass a condition. The button enables and disables itself as the underlying data changes.
Button("Send") { send() }
.disabled(message.isEmpty)Tied to State
Pair disabled with a state value and SwiftUI re-evaluates it on every change. The button reacts the moment your data updates.
@State private var name = ""
Button("Save") { save() }
.disabled(name.isEmpty)The Dimmed Look
A disabled button automatically appears dimmed. This visual cue tells people it is not ready yet, with no extra styling from you.
Validating a Form
A classic use is form validation: keep the action button off until every required field passes its check.
Button("Register") { register() }
.disabled(!isFormValid)Preventing Double Taps
During a slow task, disable the button so users cannot tap it again. Flip a loading flag while the work is running.
Button("Upload") { startUpload() }
.disabled(isLoading)Disabling a Whole Group
Apply disabled to a container and every control inside inherits it. One modifier can lock down an entire section at once.
VStack {
Button("A") { a() }
Button("B") { b() }
}
.disabled(isLocked)Children Cannot Re-enable
Once a parent is disabled, a child cannot turn itself back on. The most restrictive disabled value in the chain wins.
Disabled Still Communicates
A disabled button is not broken; it is honest. It signals that an action is unavailable right now, guiding users toward what to do first.
Enable by Flipping the Value
To re-enable, simply make the condition false again. Update the state it depends on and SwiftUI brings the button back to life.
Button("Confirm") { confirm() }
.disabled(!agreedToTerms)Quick Check
One last check on enabling and disabling.
Recap
Excellent! The disabled modifier locks a button based on a condition, dims it automatically, and re-enables when the value flips back. 🚦
Frequently Asked Questions
Is the “Disabling & Enabling Buttons” lesson free?
Yes — the full text of “Disabling & Enabling Buttons” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO. The SwiftUI Academy course includes 4 lessons in total.
What will I learn in “Disabling & Enabling Buttons”?
Control interactivity with the disabled modifier. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “Disabling & Enabling Buttons” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Creating a Tappable Button
- Styling Buttons
- Running Code on Tap
- Disabling & Enabling Buttons