[英]How to disable Customized UIButton
I subclassed UIButton
and did some customized drawing in drawRect
method such as drawing NSAttributedString
and UIImage
. 我将
UIButton
子类化,并在drawRect
方法中进行了一些自定义绘制,例如绘制NSAttributedString
和UIImage
。
However, after I did this, the customized UIButton
doesn't gray out when enabled
is set to NO
. 但是,执行完此操作后,将
enabled
的自定义UIButton
设置为NO
不会变灰。 I think my customized drawing happens on top of its state. 我认为我的自定义图纸发生在其状态之上。 How do I deal with this?
我该如何处理?
Sharing my drawing code here: 在这里分享我的绘图代码:
- (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];
}
You might have to add an overlay view with grey color and some alpha yourself. 您可能必须自己添加带有灰色和一些Alpha的覆盖视图。 Remove the overlay when button is enabled again.
再次启用按钮时,请移除覆盖层。
You can add this method to CustomButton class. 您可以将此方法添加到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];
}
For enable or disable call- 要启用或禁用通话,
[customButtob setEnabled:buttonStatus];
If you want to change color, add a background layer, and toggle its hidden property in setEnabled method. 如果要更改颜色,请添加背景图层,然后在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];
You can use 您可以使用
btn.userInteractionEnabled = NO;
Or you can use 或者你可以使用
btn.enabled = NO;
Hope this helps... :) 希望这可以帮助... :)
Edit: 编辑:
I think you can use this 我想你可以用这个
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
Add this: 添加:
-(void)setEnabled:(BOOL)enabled{
if (!enabled) {
self.alpha = 0.3;
}else{
self.alpha = 1.0;
}
[super setEnabled:enabled];
} }
I think you share some more details of your implementation. 我认为您会分享一些实施细节。 Moreover I am sure you have called
而且我确定你打过电话
Super methods in the customized methods at first call.
You have to override the setEnabled:
method of your CustomButton
您必须重写
CustomButton
的setEnabled:
方法
-(void)setEnabled:(BOOL)enabled
{
if (!enabled) {
[self setAlpha:0.2f];
}
}
OR 要么
If you have to change the color of UIButton in its disabled state. 如果必须在禁用状态下更改UIButton的颜色。 Yo can change
drawRect:
method as 可以更改
drawRect:
方法为
Make a assign
property of BOOL
type variable (say isEnabled ) 设置
BOOL
类型变量的assign
属性(例如isEnabled )
-(void)drawRect:(CGRect)rect
{
[[UIColor redColor] setFill];
UIRectFill(rect);
if (!self.isEnabled) {
[[UIColor blackColor] setFill];
UIRectFill(rect);
}
}
-(void)setEnabled:(BOOL)enabled
{
self.isEnabled = enabled;
[self setNeedsDisplay];
}
add a method which can help u for enable and disable of your button just add this method in custom button class 添加一个可以帮助您启用和禁用按钮的方法,只需在自定义按钮类中添加此方法
//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;
}
}
in the controller where u are using this button 在您正在使用此按钮的控制器中
//some where in the controller u want to disable the button just do like this
//to disable
[button makeButtonDisable:YES]; //button is an instance of customCell
//to enable
[button makeButtonDisable:NO]; //button is an instance of customCell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.