Disable button

I want to disable the ( UIButton ) button on iOS after pressing it. I am new to iOS development, but I think the equivalent code on the lens is C:

 button.enabled = NO; 

But I could not do it quickly.

+74
ios8 swift uibutton
Aug 27 '14 at 10:22
source share
8 answers

The boolean value for NO in Swift is false .

 button.isEnabled = false 

must do it.

Here is the Swift documentation for the UIControl isEnabled property.

+162
Aug 27 '14 at 10:26
source share

If you want the button to remain stationary without a “pressed” view:

 // Swift 2 editButton.userInteractionEnabled = false // Swift 3 editButton.isUserInteractionEnabled = false 



Remember:

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

2) The code above is in viewWillAppear

+33
Mar 04 '15 at 7:20
source share

How do i do this:

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

IBAction is connected to your button in the storyboard.

If you have your button installed as an output:

  @IBOutlet weak var myButton: UIButton! 

Then you can access the included properties with. button name designation:

  myButton.enabled = false 
+9
Sep 03 '15 at 10:41
source share

Disable the Swift 3 button:

 yourButton.isEnabled = false 
+5
Oct 05 '16 at 13:03
source share

You can disable a button in Swift 4 by code

 @IBAction func yourButtonMethodname(sender: UIButon) { yourButton.isEnabled = false } 
0
Apr 30 '18 at 5:37
source share

You can enable / disable the button using the isEnabled or isUserInteractionEnabled .

The difference between the two:

  • isEnabled is a UIControl property (superclass of UIButton) and has visual effects (i.e. grayed out) on / off

  • isUserInteractionEnabled is a property of UIView (UIControl superclass) and does not have a visual effect, but it does achieve its purpose

Usage:

 myButton.isEnabled = false // Recommended approach myButton.isUserInteractionEnabled = false // Alternative approach 
0
Jun 14 '18 at 6:32
source share

Suppose in Swift 4 you have a button configured to transition as an IBAction for example, @IBAction func nextLevel(_ sender: UIButton) {} and you have other actions that occur in your application (for example, a timer, gamePlay, etc. e.), Instead of disabling the transition button, you may want to give your user the opportunity to use this transition while other actions are still taking place and WITHOUT APPLYING . 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 most likely have other conditions, if appMode == 0 and / or if appMode == 1 will still occur and DO NOT conflict with the mySegue button. Thus avoiding a collision .

0
03 Feb '19 at 19:11
source share

For those who have Googled “disabled the button,” but may have more subtle use cases:

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

 yourButton.isEnabled = false 

Disable without visual effect. Do you use the button if it should look normal, but does not respond to touch? Try this!

 yourButton.userInteractionEnabled = false 

Hide without disconnecting: this approach hides the button without disconnecting it (invisibly, but you can still click):

  yourButton.alpha = 0.0 

Delete: This will completely remove the view:

  yourButton.removeFromSuperView() 

Press something after the button: the two buttons are folded and you want the top button to temporarily work as if it weren’t there? If you no longer need the top button, remove it. If you need it again, try reducing its height or width to 0!

0
May 26 '19 at 13:37
source share



All Articles