Ios 是否有其他选项可以使UIButton看起来禁用?
我正在尝试使用以下代码使UIButton看起来被禁用:Ios 是否有其他选项可以使UIButton看起来禁用?,ios,user-interface,uibutton,user-interaction,Ios,User Interface,Uibutton,User Interaction,我正在尝试使用以下代码使UIButton看起来被禁用: btnYourButton.alpha = 0.6f; btnYourButton.enabled = NO; 当Make处于启用状态时(当然也要看起来处于启用状态) 是否有任何方法可以在一条语句中同时执行这两项操作(使UIButton禁用/启用,并使其看起来禁用/启用)?或者您可以尝试将UIButton子类化,例如: 文件MyKindOfButton.h #import <UIKit/UIKit.h> @interface
btnYourButton.alpha = 0.6f;
btnYourButton.enabled = NO;
当Make处于启用状态时(当然也要看起来处于启用状态)
是否有任何方法可以在一条语句中同时执行这两项操作(使UIButton禁用/启用,并使其看起来禁用/启用)?或者您可以尝试将UIButton子类化,例如: 文件MyKindOfButton.h
#import <UIKit/UIKit.h>
@interface MyKindOfButton : UIButton
@end
另一个选项是更改禁用状态的文本颜色(例如浅灰色)。在情节提要编辑器中,从状态配置弹出按钮中选择禁用,然后使用文本颜色弹出按钮更改文本颜色。在代码中,使用-setTitleColor:forState:message
(我知道这是一篇老文章,但我想其他人可能会喜欢这个选项)我知道这是一个非常老的问题,但这里有一个非常好的解决方案。只需创建UIColor类别并添加此方法
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, (CGRect){.size = size});
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)imageWithColor:(UIColor *)color
{
return [UIImage imageWithColor:color size:CGSizeMake(1, 1)];
}
现在,您可以将背景图像设置为您想要的任何颜色,它将自动为您处理禁用外观
[button setTitleColor:[UIColor someColor] forState:UIControlStateNormal];
+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, (CGRect){.size = size});
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
+ (UIImage *)imageWithColor:(UIColor *)color
{
return [UIImage imageWithColor:color size:CGSizeMake(1, 1)];
}
[button setTitleColor:[UIColor someColor] forState:UIControlStateNormal];