Reputation: 785
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?
Upvotes: 36
Views: 18103
Reputation: 1953
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...
Upvotes: 2
Reputation: 4624
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.
Upvotes: 1
Reputation: 83
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."
Upvotes: 1
Reputation: 8068
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.
Upvotes: 51
Reputation: 4066
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 ;)
Upvotes: 44