[swift] Disable a Button

I want to disable a button (UIButton) on iOS after it is clicked. I am new to developing for iOS but I think the equivalent code on objective - C is this:

button.enabled = NO;

But I couldn't do that on swift.

This question is related to swift uibutton ios8

The answer is


in order for this to work:

yourButton.isEnabled = false

you need to create an outlet in addition to your UI button.


Disable a button on Swift 3:

yourButton.isEnabled = false

The way I do this is as follows:

@IBAction func pressButton(sender: AnyObject) {
    var disableMyButton = sender as? UIButton
    disableMyButton.enabled = false
}

The IBAction is connected to your button in the storyboard.

If you have your button setup as an Outlet:

    @IBOutlet weak var myButton: UIButton!

Then you can access the enabled properties by using the . notation on the button name:

    myButton.enabled = false

Let's say in Swift 4 you have a button set up for a segue as an IBAction like this @IBAction func nextLevel(_ sender: UIButton) {} and you have other actions occurring within your app (i.e. a timer, gamePlay, etc.). Rather than disabling the segue button, you might want to give your user the option to use that segue while the other actions are still occurring and WITHOUT CRASHING THE APP. Here's how:

var appMode = 0

@IBAction func mySegue(_ sender: UIButton) {

    if appMode == 1 {  // avoid crash if button pressed during other app actions and/or conditions
        let conflictingAction = sender as UIButton
        conflictingAction.isEnabled = false
    }
}

Please note that you will likely have other conditions within if appMode == 0 and/or if appMode == 1 that will still occur and NOT conflict with the mySegue button. Thus, AVOIDING A CRASH.


The button can be Disabled in Swift 4 by the code

@IBAction func yourButtonMethodname(sender: UIButon) {
 yourButton.isEnabled = false 
}

Swift 5 / SwiftUI

Nowadays it's done like this.

Button(action: action) {
  Text(buttonLabel)
}
  .disabled(!isEnabled)

You can enable/disable a button using isEnabled or isUserInteractionEnabled property.

The difference between two is :

  • isEnabled is a property of UIControl (super class of UIButton) and it has visual effects (i.e. grayed out) of enable/disable

  • isUserInteractionEnabled is a property of UIView (super class of UIControl) and has no visual effect although but achieves the purpose

Usage :

myButton.isEnabled = false // Recommended approach

myButton.isUserInteractionEnabled = false // Alternative approach

For those who Googled "disable a button" but may have more nuanced use cases:

Disable with visual effect: As others have said, this will prevent the button from being pressed and the system will automatically make it look disabled:

yourButton.isEnabled = false 

Disable without visual effect: Are you using a button in a case where it should look normal but not behave likes button by reacting to touches? Try this!

yourButton.userInteractionEnabled = false

Hide without disabling: This approach hides the button without disabling it (invisible but can still be tapped):

 yourButton.alpha = 0.0

Remove: This will remove the view entirely:

 yourButton.removeFromSuperView()

Tap something behind a button: Have two buttons stacked and you want the top button to temporarily act like it's not there? If you won't need the top button again, remove it. If you will need it again, try condensing its height or width to 0!


If you want the button to stay static without the "pressed" appearance:

// Swift 2
editButton.userInteractionEnabled = false 

// Swift 3
editButton.isUserInteractionEnabled = false 

Remember:

1) Your IBOutlet is --> @IBOutlet weak var editButton: UIButton!

2) Code above goes in viewWillAppear


Examples related to swift

Make a VStack fill the width of the screen in SwiftUI Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code in Xcode 10 Convert Json string to Json object in Swift 4 iOS Swift - Get the Current Local Time and Date Timestamp Xcode 9 Swift Language Version (SWIFT_VERSION) How do I use Safe Area Layout programmatically? How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator Safe Area of Xcode 9 The use of Swift 3 @objc inference in Swift 4 mode is deprecated?

Examples related to uibutton

How to make a simple rounded button in Storyboard? How to set the title text color of UIButton? How can I make a button have a rounded border in Swift? How to change UIButton image in Swift Changing text of UIButton programmatically swift Disable a Button How to change font of UIButton with Swift Attach parameter to button.addTarget action in Swift Change button background color using swift language Make a UIButton programmatically in Swift

Examples related to ios8

Is there any way to show a countdown on the lockscreen of iphone? How to display an activity indicator with text on iOS 8 with Swift? How to call gesture tap on UIView programmatically in swift Changing the Status Bar Color for specific ViewControllers using Swift in iOS8 Can I set the cookies to be used by a WKWebView? UIAlertController custom font, size, color How to force view controller orientation in iOS 8? How to get textLabel of selected row in swift? Launch Image does not show up in my iOS App NavigationBar bar, tint, and title text color in iOS 8