Disabled UIButton not faded or gray

In my iPhone application, I have a UIButton that I created in Interface Builder. I can successfully enable and disable it, as it is in my code ...

sendButton.enabled = YES; 

or

 sendButton.enabled = NO; 

However, the visual appearance of the button is always the same! It is not faded or gray. If I try to click it, it will be turned on or off as expected. Am I missing something? Shouldn't it look faded or gray?

thank

+124
ios iphone cocoa-touch uibutton
Apr 19 2018-11-11T00:
source share
17 answers

You can use the following code:

 sendButton.enabled = YES; sendButton.alpha = 1.0; or sendButton.enabled = NO; sendButton.alpha = 0.5; 
+169
Apr 20 '11 at 3:24 a.m.
source share

just change the state configuration to disable and select what you want, background image for disabled state

enter image description here

+70
Aug 28 '15 at 8:35
source share

Another option is to change the text color (for example, light gray) for the disabled state. In the storyboard editor, select "Disabled" from the "Status Configuration" pop-up button and use the "Text Color" button to change the color of the text. In the code, use the -setTitleColor: forState: message.

(I understand this is an older post, but I thought others might like this option)

+32
Jan 27 '13 at 3:00
source share

Try setting different images for UIControlStateDisabled (gray images disabled) and UIControlStateNormal (normal image) so that the button generates a disabled state for you.

+23
Apr 19 2018-11-11T00:
source share

If you use a text button, you can put an instance method in viewDidLoad

 - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state 

Example:

 [self.syncImagesButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; 
+12
Oct 05 '13 at 16:08 on
source share

To make the button faded when disabled, you can set the alpha for it. There are two options for you:

The first way : if you want to apply for all your buttons in your application, you can write an extension for UIButton as follows:

 extension UIButton { open override var isEnabled: Bool{ didSet { alpha = isEnabled ? 1.0 : 0.5 } } } 

The second way : if you just want to apply for some buttons in your application, you can write your own class from UIButton, as shown below, and use this class for which you want to apply:

 class MyButton: UIButton { override var isEnabled: Bool { didSet { alpha = isEnabled ? 1.0 : 0.5 } } } 
+11
Feb 28 '18 at 7:59
source share

You can use adjustsImageWhenDisabled , which is a UIButton property
(@property (nonatomic) BOOL adjustsImageWhenDisabled)

Example:

  Button.adjustsImageWhenDisabled = false 
+10
02 Feb '16 at 13:12
source share

This is a pretty old question, but there is an easy way to achieve this.

myButton.userInteractionEnabled = false

This will disable any touch gestures without changing the appearance of the button.

+6
Jun 08 '16 at 16:20
source share

In Swift :

 previousCustomButton.enabled = false previousCustomButton.alpha = 0.5 

or

 nextCustomButton.enabled = true nextCustomButton.alpha = 1.0 
+4
Nov 23 '14 at 12:42 on
source share

You can use both first answers and become the best result.

In the attribute inspector (when choosing a button), change the "Status configuration" to "Disabled" to set a new image that will be displayed when it is disabled (remove the marker in the "Content-> Enabled" window to make it disabled).

And when you change the state to enabled, the image will load one of this state.

Adding alpha logic to this is a good detail.

Hello!

+3
May 20 '13 at 22:54
source share

Create an extension in Swift> 3.0, not every button

 extension UIButton { override open var isEnabled : Bool { willSet{ if newValue == false { self.setTitleColor(UIColor.gray, for: UIControlState.disabled) } } } 
+3
Oct 19 '17 at 11:18
source share

I am stuck in the same problem. Tuning alpha is not what I want.

UIButton has a "background image" and a "background color". For an image, UIButton has a property like

@property (nonatomic) BOOL adjustsImageWhenDisabled

And the background image becomes brighter when the button is off. For the background color, setting the Ravin alpha solution works great.

First, you should check to see if the "disabled image settings" checkbox in the "Attributes-Utilities" section has been unchecked.
Then check the background color for this button.

(Hope this is helpful. This is an old post, everything can change in Xcode).

+2
Apr 21 '15 at 22:59
source share

Set title color for different states:

 @IBOutlet weak var loginButton: UIButton! { didSet { loginButton.setTitleColor(UIColor.init(white: 1, alpha: 0.3), for: .disabled) loginButton.setTitleColor(UIColor.init(white: 1, alpha: 1), for: .normal) } } 

Usage: (text color will be changed automatically)

 loginButton.isEnabled = false 

enter image description here

+2
Aug 30 '18 at 6:42
source share

If the UIButton is in a state combined with selected and disabled , this state is UIControlStateSelected | UIControlStateDisabled UIControlStateSelected | UIControlStateDisabled .

So he has to set up

 [button setTitleColor:color UIControlStateSelected | UIControlStateDisabled]; [button setImage:image forState:UIControlStateSelected | UIControlStateDisabled]; 



The approach is a subclass of UIButton as follows.

 @implementation TTButton - (void)awaeFromNib { [super awakeFromNib]; //! Fix behavior when `disabled && selected` //! Load settings in `xib` or `storyboard`, and apply it again. UIColor *color = [self titleColorForState:UIControlStateDisabled]; [self setTitleColor:color forState:UIControlStateDisabled]; UIImage *img = [self imageForState:UIControlStateDisabled]; [self setImage:img forState:UIControlStateDisabled]; } - (void)setTitleColor:(UIColor *)color forState:(UIControlState)state { [super setTitleColor:color forState:state]; // if (UIControlStateDisabled == state) { [super setTitleColor:color forState:UIControlStateSelected | state]; [super setTitleColor:color forState:UIControlStateHighlighted | state]; } } - (void)setImage:(UIImage *)image forState:(UIControlState)state { [super setImage:image forState:state]; if (UIControlStateDisabled == state) { [super setImage:image forState:UIControlStateSelected | state]; [super setImage:image forState:UIControlStateHighlighted | state]; } } @end 
+1
Sep 07 '16 at 6:37
source share

It looks like the following code is working fine. But in some cases this does not work.

 sendButton.enabled = NO; sendButton.alpha = 0.5; 

If the code above does not work, put it in the main thread. So

 dispatch_async(dispatch_get_main_queue(), ^{ sendButton.enabled = NO; sendButton.alpha = 0.5 }); 

if you go with fast like this

 DispatchQueue.main.async { sendButton.isEnabled = false sendButton.alpha = 0.5 } 

Also, if you configured UIButton, add this to the Button class.

 override var isEnabled: Bool { didSet { DispatchQueue.main.async { if self.isEnabled { self.alpha = 1.0 } else { self.alpha = 0.5 } } } } 

Thanks and enjoy the coding !!!

+1
Mar 14 '19 at 12:28
source share

There are many answers to this question, but they all do not look very useful if you really want to use backgroundColor to style your buttons. UIButton has a good option to set different images for different control states, but there is no other function for background colors. Therefore, one solution is to add an extension that will generate images from the color and apply them to the button.

 extension UIButton { private func image(withColor color: UIColor) -> UIImage? { let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() context?.setFillColor(color.cgColor) context?.fill(rect) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } func setBackgroundColor(_ color: UIColor, for state: UIControlState) { self.setBackgroundImage(image(withColor: color), for: state) } } 

Only one problem with this solution is that this change will not be applied to buttons created in the storyboard. For me, this is not a problem, because I prefer to style the user interface from code. If you want to use storyboards, then you need extra magic with @IBInspectable .

The second option is to create subclasses, but I prefer to avoid this.

0
Aug 15 '18 at 14:40
source share
 #import "UIButton+My.h" #import <QuartzCore/QuartzCore.h> @implementation UIButton (My) -(void)fade :(BOOL)enable{ self.enabled=enable;// self.alpha=enable?1.0:0.5; } @end 

.h:

 #import <UIKit/UIKit.h> @interface UIButton (My) -(void)fade :(BOOL)enable; @end 
-one
Dec 04 '14 at 8:43
source share



All Articles