简体   繁体   English

UIButton 不会变灰

[英]UIButton won't gray out

Isn't UIButton supposed to become grayish/grayer when enabled=NO? UIButton 在启用=否时不应该变成灰色/灰色吗?

I have a simple UIButton on a blackbackground (no custom images, no custom nothing, just dragged it with IB and changed size and title).我在黑色背景上有一个简单的 UIButton(没有自定义图像,没有自定义任何内容,只是用 IB 拖动它并更改了大小和标题)。

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...现在我正在使用一个小的愚蠢的解决方法: hidden blackbg 0,5 alpha UIView 在我需要禁用按钮时变为 hidden=NO 的按钮顶部......但我想正确设置按钮......

Any thoughts?有什么想法吗?

There is another way without having to alpha the whole button:还有另一种方法,无需对整个按钮进行 alpha 处理:

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

Then whenever you set the enabled property to NO, the button's text will automatically gray out.然后,每当您将 enabled 属性设置为 NO 时,按钮的文本将自动变灰。

There is no way to make a UIButton "grayer".没有办法使 UIButton “变灰”。 But you can use that trick:但是你可以使用这个技巧:

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

So your UIButton looks like unusable;)因此,您的 UIButton 看起来无法使用;)

I happened upon this question and Apple has published a new UIKit User Interface Catalog for working with Buttons in iOS 7.我遇到了这个问题,Apple 发布了一个新的UIKit 用户界面目录,用于使用 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."针对您的问题, UIButton Class现在公开了一个名为 adjustsImageWhenDisabled 的属性,它是“一个 Boolean 值,用于确定按钮被禁用时是否更改图像。”

If this adjustsImageWhenDisabled property is set to "YES, the image is drawn darker when the button is disabled. The default value is YES."如果将此 adjustsImageWhenDisabled 属性设置为“YES,则当按钮被禁用时,图像会被绘制得更暗。默认值为 YES。”

Simply make a UIButton category like the following and import #import "UIButton+StateColors.h" in the classes where you want to use it.只需创建一个 UIButton 类别,如下所示,然后在要使用它的类中导入#import "UIButton+StateColors.h"

.h 。H

#import <UIKit/UIKit.h>

@interface UIButton (StateColors)

-(void)makeDisabled:(BOOL)flag;

@end

.m .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...我希望这是一个通用的解决方案...

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.我删除了背景颜色并将其设置为仅UIControlStateNormal并且启用/禁用的默认行为开始出现。

If you are setting background color instead of image try this category for converting UIColor to UIImage:如果您设置背景颜色而不是图像,请尝试使用此类别将 UIColor 转换为 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.现在,当您启用/禁用时,应该会出现灰色效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM