Set Background Color on UIButton when Disabled?

23.7k Views Asked by At

Is it possible to set the background color of a disabled UIButton? We'd like to use a more muted color when the button is disabled, but I do not see any methods within UIButton that would allow this. Is there a workaround or another way we could accomplish this?

6

There are 6 best solutions below

3
On

Hope this helps :

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 35)];

CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[btn setBackgroundImage:image forState:UIControlStateDisabled];
0
On

As suggested here: https://stackoverflow.com/a/5725169/1586606
Set alpha property like;

myButton.alpha = 0.5 
0
On

This code is for Swift 2

    let rect = CGRectMake(0, 0, someButton.frame.width,someButton.frame.height)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()
    CGContextSetFillColor(context, CGColorGetComponents(UIColor.lightGrayColor().CGColor))
    CGContextFillRect(context, rect)
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0)
    view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    someButton.setBackgroundImage(image, forState: .Disabled)

    someButton.enabled = false
0
On

I am concentrating on this comment of your's -"We'd like to use a more muted color when the button is disabled". If changing the alpha of the button is an acceptable solution you can very well use this code.

someButton.alpha  = 0.5;//This code should be written in the place where the button is disabled.
0
On

Seems that this is not possible as the background color property is controlled by the UIView class which is the parent of UIButton.

So UIButton control state doesn't own this property and doesn't control it.

But still you can inherit UIButton and overwrite the setEnabled method to update the color accordingly.

This is the most clear way to do it. as you can assign the inherited class to any UIButton in storyboard or Xib file. without the need for any connections. (no IBOutlets)

Hope this is helpful, if you wish I can share some code.

1
On

The best way to work with button, in my honest opinion, is subclassing and customize UI, like in example:

class XYButton: UIButton {

    override public var isEnabled: Bool {
        didSet {
            if self.isEnabled {
                self.backgroundColor = self.backgroundColor?.withAlphaComponent(1.0)
            } else {
                self.backgroundColor = self.backgroundColor?.withAlphaComponent(0.5)
            }
        }
    }

}

Then you can use XYButton either in your storyboards/xibs or programmatically.

** more elegant:

didSet {
    self.backgroundColor = self.backgroundColor?.withAlphaComponent(isEnabled ? 1.0 : 0.5)
}