Enable and Disable Button in SwiftUI

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

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.



✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



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.

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