36

Isn't UIButton supposed to become grayish/grayer when enabled=NO ?

I have a simple UIButton on a blackbackground (no custom images, no custom nothing, just dragged it with IB and changed size and title).

And when I set it programatically to become disabled it stays white as hell!

For now I'm using a small stupid workaround: hidden blackbg 0,5 alpha UIView on top of the button that becomes hidden=NO when I need to disable the button... but I would like to set the button properly...

Any thoughts?

THelper
  • 15,333
  • 6
  • 64
  • 104
xfze
  • 765
  • 1
  • 9
  • 24

5 Answers5

51

There is another way without having to alpha the whole button:

[startButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];

Then whenever you set the enabled property to NO, the button's text will automatically gray out.

jowie
  • 8,028
  • 8
  • 55
  • 94
44

There is no way to make a UIButton "grayer". But you can use that trick :

UIButton *myButton;
myButton.alpha = 0.4;
myButton.enabled = NO;

So your UIButton looks like unusable ;)

Pierre Espenan
  • 3,996
  • 5
  • 33
  • 51
  • 1
    yes, since the UIButton is on top of a black view, changing the alpha gives the exact same effect as my black UIView, but it's a much more clever and simple solution, should have thought of that first - I'm switching ;) thank you – xfze Jun 10 '11 at 16:37
  • Simple good answer. It's actually 0.5 alpha if you want it to look exactly like a disabled button. – Eran Talmor Sep 07 '21 at 09:46
2

Simply make a UIButton category like the following and import #import "UIButton+StateColors.h" in the classes where you want to use it.

.h

#import <UIKit/UIKit.h>

@interface UIButton (StateColors)

-(void)makeDisabled:(BOOL)flag;

@end

.m

#import "UIButton+StateColors.h"

#define ENABLED_BUTTON_ALPHA 1
#define DISABLED_BUTTON_ALPHA 0.3

@implementation UIButton (StateColors)

-(void)makeDisabled:(BOOL)flag {
    self.enabled = !flag;
    self.alpha = flag ? DISABLED_BUTTON_ALPHA : ENABLED_BUTTON_ALPHA;
}


@end

And use it like this...

[self.emailBtn makeDisabled:NO];
[self.printBtn makeDisabled:YES];

It's a universal solution I hope...

Muhammad Ibrahim
  • 1,923
  • 2
  • 15
  • 13
1

I happened upon this question and Apple has published a new UIKit User Interface Catalog for working with Buttons in iOS 7.

In response to your question, the UIButton Class now exposes a property called adjustsImageWhenDisabled, which is "a Boolean value that determines whether the image changes when the button is disabled."

If this adjustsImageWhenDisabled property is set to "YES, the image is drawn darker when the button is disabled. The default value is YES."

user2728981
  • 83
  • 1
  • 5
1

I face the same problem because I had set background color.

I removed the background color and set it for UIControlStateNormal only and the default behaviour for enable/disable started to appear.

If you are setting background color instead of image try this category for converting UIColor to UIImage:

copied from here:

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

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

    return image;
}

then use this:

[self.loginButton setBackgroundImage:[UIImage imageWithColor:greenColor] forState:UIControlStateNormal];
self.loginButton.enabled = NO;

to set the color as background. Now when you enable/disable, the gray effect should appear.

Community
  • 1
  • 1
Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65