Enable and Disable Button in SwiftUI

Uplift iOS Interview

"Uplift iOS Interview" is a comprehensive guide to help aspiring iOS developers soar to new heights in their careers. This book is an indispensable tool for anyone looking to crack the iOS interview and impress their future employers with their technical prowess. With in-depth coverage of Swift, AutoLayout, SwiftUI, Multithreading, Memory management so on and so forth, this book is a treasure trove of knowledge for anyone looking to uplift their iOS development career.

In this tutorial, we will learn how to create an enable/disable button in SwiftUI. In the ContentView.swift file, we will create a button and a state variable that will determine whether the button is enabled or disabled.

struct ContentView: View {
    @State private var isButtonEnabled = true

    var body: some View {
        Button(action: {
            // button action here
        }) {
            Text("Button")
        }
        .disabled(!isButtonEnabled)
    }
}

In the above code, we have created a button with a text “Button” and a state variable isButtonEnabled. The disabled modifier is used to disable the button based on the value of the state variable. The exclamation mark before isButtonEnabled negates the value, so when isButtonEnabled is true, the button is enabled, and when it is false, the button is disabled.

Next, we will add a toggle switch that will allow us to enable or disable the button.

struct ContentView: View {
    @State private var isButtonEnabled = true

    var body: some View {
        VStack {
            Button(action: {
                // button action here
            }) {
                Text("Button")
            }
            .disabled(!isButtonEnabled)

            Toggle(isOn: $isButtonEnabled) {
                Text("Enable button")
            }
        }
    }
}

In the above code, we have added a toggle switch with a text “Enable button” and a binding to the isButtonEnabled state variable. This means that when the toggle switch is turned on, the value of isButtonEnabled is set to true, and the button becomes enabled. When the toggle switch is turned off, the value of isButtonEnabled is set to false, and the button becomes disabled.

Disable multiple views

It is possible to enable and disable multiple buttons and views that are located within the same container view by setting the disabled modifier on that container view.

struct ContentView: View {
    @State private var isButtonsEnabled = true

    var body: some View {
        VStack {
         VStack {
            Button(action: {
                // button action here
            }) {
                Text("Button 1")
            }

            Button(action: {
                // button action here
            }) {
                Text("Button 2")
            }
          }.disabled(!isButtonsEnabled)

            Toggle(isOn: $isButtonsEnabled) {
                Text("Enable buttons")
            }
        }
    }
}

And that’s it! With just a few lines of code, we have created an enable/disable button in SwiftUI. You can customize this further by adding more functionality or styling the button and toggle switch to your liking.


Rev Up Your iOS Skills: Take a Dynamic Learning Journey
iOS Career Boost

iOS Career Boost is the ultimate learning journey to elevate your iOS development career through a dynamic blend of visual learning, handy cheat sheets, coding practice materials, and expertly curated tips and tricks


Get Ready to Shine: Mastering the iOS Interview


Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book. Your email address will only be used for the purpose of sending the newsletter and will not be shared with third parties or advertisers. Rest assured that we value your privacy and will not spam your inbox.


Connect with me on

Twitter and LinkedIn and don't hesitate to reach out with any questions about this post. Thank you for reading.

If you know someone who would benefit from reading this article, please share it with them.