UIButton实现setBackgroundColor:ForState功能
2015-02-05 10:32
393 查看
在使用UIButton时,很多时候我们需要一个类似于- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state这样的方法,来实现在不同的状态下使用不同的backgroundColor。遗憾的是,iOS默认并没有实现这个方法,那我们就自己来实现它。
让我们先来看看对于设置BackgroundImage,UIButton提供了如下方法:
具体实现如下所示,代码很简单,
使用时,如下调用即可:
让我们先来看看对于设置BackgroundImage,UIButton提供了如下方法:
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil - (UIImage *)backgroundImageForState:(UIControlState)state;类似的,我们的函数实现声明如下:
具体实现如下所示,代码很简单,
1 2 3 4 5 @interface WMButton : UIButton @property (nonatomic, copy) NSString *name; - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state; - (UIColor *)backgroundColorForState:(UIControlState)state; @end
@implementation WMButton { NSMutableDictionary *_colors; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { _colors = [NSMutableDictionary dictionary]; } return self; } - (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { // If it is normal then set the standard background here if(state == UIControlStateNormal) { [super setBackgroundColor:backgroundColor]; } // Store the background colour for that state [_colors setValue:backgroundColor forKey:[self keyForState:state]]; } - (UIColor *)backgroundColorForState:(UIControlState)state { return [_colors valueForKey:[self keyForState:state]]; } - (void)setHighlighted:(BOOL)highlighted { // Do original Highlight [super setHighlighted:highlighted]; // Highlight with new colour OR replace with orignial NSString *highlightedKey = [self keyForState:UIControlStateHighlighted]; UIColor *highlightedColor = [_colors valueForKey:highlightedKey]; if (highlighted && highlightedColor) { [super setBackgroundColor:highlightedColor]; } else { // 由于系统在调用setSelected后,会再触发一次setHighlighted,故做如下处理,否则,背景色会被最后一次的覆盖掉。 if ([self isSelected]) { NSString *selectedKey = [self keyForState:UIControlStateSelected]; UIColor *selectedColor = [_colors valueForKey:selectedKey]; [super setBackgroundColor:selectedColor]; } else { NSString *normalKey = [self keyForState:UIControlStateNormal]; [super setBackgroundColor:[_colors valueForKey:normalKey]]; } } } - (void)setSelected:(BOOL)selected { // Do original Selected [super setSelected:selected]; // Select with new colour OR replace with orignial NSString *selectedKey = [self keyForState:UIControlStateSelected]; UIColor *selectedColor = [_colors valueForKey:selectedKey]; if (selected && selectedColor) { [super setBackgroundColor:selectedColor]; } else { NSString *normalKey = [self keyForState:UIControlStateNormal]; [super setBackgroundColor:[_colors valueForKey:normalKey]]; } } - (NSString *)keyForState:(UIControlState)state { return [NSString stringWithFormat:@"state_%d", state]; } @end
使用时,如下调用即可:
[button setBackgroundColor:[UIColor clearColor] forState:UIControlStateNormal]; [button setBackgroundColor:[UIColor clearColor] forState:UIControlStateHighlighted]; [button setBackgroundColor:HotPinkColor forState:UIControlStateSelected];
相关文章推荐
- UIButton setBackgroundColor:ForState
- iOS --- 为UIButton添加setBackgroundColor:forState:方法(包含OC和Swift两个版本)
- UIButton的setImage:forState:和setBackgroundImage:forState:方法
- UIButton setBackgroundColor:ForState
- set button background to color
- STL中关于Set的部分功能实现
- IClassBreaksRenderer接口渲染图层(分color和size两种) 实现ArcMap中的Graduated Symbols功能
- Windows Phone 7 - dynamically set button background color from Hex?
- 用唯一的颜色id编号实现OpenGL选择功能(OpenGL Selection Using Unique Color IDs )
- 解决 UIView 设置背景为UIImage图片变型问题[XXX setBackgroundColor:
- setBackground、setBackgroundColor、setBackgroundDrawable、setBackgroundResource的区别
- windows下简单的调用Setforegroundwindow并不能将窗口置最前,我找到三种方法可以实现该功能。
- setBackgroundDrawable和setBackgroundColor的用法
- 利用 Avisynth 2.5.8 的 ColorKeyMask 功能实现视频抠像
- setBackgroundDrawable和setBackgroundColor的用法
- setBackgroundDrawable和setBackgroundColor的用法
- pRichEdit->SetBackgroundColor(FALSE, oldclr); 只能设置一半 的背景色
- setBackgroundDrawable和setBackgroundColor的用法
- 利用Paint的 setColorFilter 颜色过滤 来实现按钮被按下的效果
- setBackgroundDrawable和setBackgroundColor的用法