UIButton won't gray out

戏子无情 提交于 2020-01-29 03:24:48

问题


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?


回答1:


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 ;)




回答2:


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.




回答3:


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."




回答4:


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...




回答5:


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.



来源:https://stackoverflow.com/questions/6309225/uibutton-wont-gray-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!