Ios 如何禁用自定义UIButton
我对Ios 如何禁用自定义UIButton,ios,uibutton,Ios,Uibutton,我对UIButton进行了子类化,并在drawRect方法中进行了一些自定义绘制,例如绘制NSAttributedString和UIImage 但是,在我执行此操作之后,当启用设置为否时,自定义的ui按钮不会变灰。我认为我的自定义绘图发生在它的状态之上。我该怎么处理 在此处共享我的绘图代码: - (void)drawRect:(CGRect)rect { // Drawing code if (self.faceUp) { [self drawCardText:s
UIButton
进行了子类化,并在drawRect
方法中进行了一些自定义绘制,例如绘制NSAttributedString
和UIImage
但是,在我执行此操作之后,当启用
设置为否
时,自定义的ui按钮
不会变灰。我认为我的自定义绘图发生在它的状态之上。我该怎么处理
在此处共享我的绘图代码:
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.faceUp) {
[self drawCardText:self.card.contents inRect:self.bounds];
} else {
[self drawCardImage:[UIImage imageNamed:CardBackImageName] inRect:self.bounds];
}
}
- (void)drawCardText:(NSString *)text inRect:(CGRect)rect
{
// set background color to white so text can be shown
[[UIColor whiteColor] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
UIFont *preferedFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFont *actualFont = [UIFont fontWithName:preferedFont.fontName
size:hypotf(rect.size.width, rect.size.height) / 6.0];
NSDictionary *attributes = @{NSFontAttributeName: actualFont};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
[self setAttributedTitle:attributedText forState:UIControlStateNormal];
}
- (void)drawCardImage:(UIImage *)image inRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0f);
[image drawInRect:rect];
UIImage *actualImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UIColor colorWithPatternImage:actualImage] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
}
你可以用
btn.userInteractionEnabled = NO;
btn.enabled = NO;
或者你可以使用
btn.userInteractionEnabled = NO;
btn.enabled = NO;
希望这有帮助……:)
编辑:
我想你可以用这个
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
添加以下内容:
-(void)setEnabled:(BOOL)enabled{
if (!enabled) {
self.alpha = 0.3;
}else{
self.alpha = 1.0;
}
[super setEnabled:enabled];
}您可能需要自己添加一个带有灰色和一些alpha的覆盖视图。再次启用按钮时删除覆盖。您可以将此方法添加到CustomButton类
-(void)setEnabled:(BOOL)enabled{
//disableLayer.hidden = !enabled;
if (enabled) {
//self.enabled = YES;
self.alpha = 1.0;
}else{
//self.enabled = NO;
self.alpha = 0.7;
}
[super setEnabled:enabled];
}
用于启用或禁用调用-
[customButtob setEnabled:buttonStatus];
如果要更改颜色,请添加背景层,并在setEnabled方法中切换其隐藏属性
disableLayer = [CALayer layer];
disableLayer.backgroundColor = [UIColor colorWithRed:20/255.0f green:20/255.0f blue:20/255.0f alpha:1.0f].CGColor;
disableLayer.frame = self.layer.bounds;
disableLayer.hidden = YES;
[self.layer insertSublayer:disableLayer below:otherLayer];
我想你可以分享一些关于你的实现的更多细节。而且我相信你已经打过电话了
Super methods in the customized methods at first call.
您必须覆盖
CustomButton的setEnabled:
方法
-(void)setEnabled:(BOOL)enabled
{
if (!enabled) {
[self setAlpha:0.2f];
}
}
或
如果必须在禁用状态下更改UIButton的颜色。您可以将drawRect:
方法更改为
为BOOL
类型变量创建assign
属性(例如isEnabled)
添加一个方法,它可以帮助您启用和禁用您的按钮,只需在自定义按钮类中添加此方法
//in customButton.h file
#import <UIKit/UIKit.h>
@interface CustomButton : UIButton
@property (nonatomic,assign) BOOL faceUp;//your property
- (void)makeButtonDisable:(BOOL)disable;//method to handle disable and enable
@end
//in customButton.m file
#import "CustomButton.h"
@implementation CustomButton
{
CALayer *grayLayer;//layer to handle disable and enable
}
@synthesize faceUp;
// in initilization method
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
grayLayer = [CALayer layer];
grayLayer.frame = frame;
[self.layer addSublayer:grayLayer]; //add the grayLayer during initialisation
}
return self;
}
//your code that put above
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.faceUp) {
[self drawCardText:self.card.contents inRect:self.bounds];
} else {
[self drawCardImage:[UIImage imageNamed:CardBackImageName] inRect:self.bounds];
}
}
- (void)drawCardText:(NSString *)text inRect:(CGRect)rect
{
// set background color to white so text can be shown
[[UIColor whiteColor] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
UIFont *preferedFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
UIFont *actualFont = [UIFont fontWithName:preferedFont.fontName
size:hypotf(rect.size.width, rect.size.height) / 6.0];
NSDictionary *attributes = @{NSFontAttributeName: actualFont};
NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:attributes];
[self setAttributedTitle:attributedText forState:UIControlStateNormal];
}
- (void)drawCardImage:(UIImage *)image inRect:(CGRect)rect
{
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0f);
[image drawInRect:rect];
UIImage *actualImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UIColor colorWithPatternImage:actualImage] setFill];
[[UIBezierPath bezierPathWithRect:rect] fill];
}
//add this method
- (void)makeButtonDisable:(BOOL)disable
{
if(disable)
{
grayLayer.backgroundColor = [UIColor colorWithRed:201.0f/255.0f green:201.0f/255.0f blue:201.0f/255.0f alpha:5.0f].CGColor;
self.userInteractionEnabled = NO;
grayLayer.opacity = 0.5f;
self.alpha = 0.5f;
}
else
{
grayLayer.backgroundColor = [UIColor clearColor].CGColor;
self.userInteractionEnabled = YES;
grayLayer.opacity = 0.0f;
self.alpha = 1.0f;
}
}
我认为这个问题是不同的。当按钮被禁用时,它看起来不像禁用(像灰色表示禁用)。@bohanl Ya我理解。你能展示一下你的绘图代码吗?@Mani,更新了这个问题,让它更简洁confusing@Rashad,共享了上面的我的代码,[btn setTitleColor:[UIColor grayColor]用于状态:UIControlStateDisabled]代码>不起作用。我试过了。你能显示你的代码吗?我想在drawRect:上添加一个检查按钮是否被禁用,并在那里正常绘制灰色的东西。我不确定是否在已启用<代码>的设置程序中实现了<代码>设置需要显示代码>。否则,当我将enabled
设置为NO
时,它如何知道重新绘制?@bohanl看到我的答案,可能会对您有所帮助。@bohanl覆盖setEnabled并调用setneedsdplay,我想。好的一点,我确实忘记了。但是添加[super-drawRect:rect]
没有帮助。