您的位置:首页 > 产品设计 > UI/UE

UIButton实现setBackgroundColor:ForState功能

2015-02-05 10:32 393 查看
在使用UIButton时,很多时候我们需要一个类似于- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state这样的方法,来实现在不同的状态下使用不同的backgroundColor。遗憾的是,iOS默认并没有实现这个方法,那我们就自己来实现它。

让我们先来看看对于设置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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: