iOS UIButton按钮添加按状态设置背景色的方法

前言

最近经常遇到一些需求,需要在按钮不可点击的状态下置灰,之前通常会用下面一个方法来代替,其中image是由纯色转变成的图片。

//其中image是由纯色转变成的图片 
 [button setBackgroundImage:image forState:UIControlStateSelectd];
 button.userInteractionEnabled = NO;

但是觉得这种方法需要的东西太多不够优化,又是很low,又是需要两行代码,于是想找个方法优化一下,决定封装一个类别,一次封装永久使用。于是网上查了一些资料,提供思路,参考《iOS --- 为UIButton添加setBackgroundColor:forState:方法(包含OC和Swift两个版本)》,但这篇文章也不能完全解决上述问题,就进一步进行了优化,其中用到了runtime机制,有不熟悉的小伙伴可以底下学一下。多了不说,直接上代码。
.h文件

#import 

NS_ASSUME_NONNULL_BEGIN


@interface UIButton (YYPExtension)

/**
 存放颜色和状态字符串的字典
 */
@property(nonatomic, strong) NSMutableDictionary *stateBackgroundColor;

/**
 根据状态设置背景颜色

 @param backgroundColor 背景色
 @param state 状态
 */
- (void)setBackgroundColor:(UIColor *)backgroundColor state:(UIControlState)state;

/**
 设置不可点击下的背景色

 @param backgroundColor 背景色
 */
- (void)setUserInteractionDisabledBackgroundColor:(UIColor *)backgroundColor;
@end

NS_ASSUME_NONNULL_END

.m文件

#import "UIButton+YYPExtension.h"


static const char * stateBackgroundColorID = "stateBackgroundColorID";

static NSString *yypNormal = @"yypNormal";

static NSString *yypHighlighted = @"yypHighlighted";

static NSString *yypDisabled = @"yypDisabled";

static NSString *yypSelected = @"yypSelected";

static NSString *yypUserDisabled = @"yypUserDisabled";


@implementation UIButton (YYPExtension) 

- (void)setStateBackgroundColor:(NSMutableDictionary *)stateBackgroundColor {
    objc_setAssociatedObject(self, stateBackgroundColorID, stateBackgroundColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}
- (NSMutableDictionary *)stateBackgroundColor {
    return objc_getAssociatedObject(self, stateBackgroundColorID);

}
- (void)setBackgroundColor:(UIColor *)backgroundColor state:(UIControlState)state {
    if (!self.stateBackgroundColor) {
        self.stateBackgroundColor = [NSMutableDictionary dictionary];
    }
    [self.stateBackgroundColor setObject:backgroundColor forKey:[self yyp_stringForUIControlState:state]];
}
- (void)setUserInteractionDisabledBackgroundColor:(UIColor *)backgroundColor {
    if (!self.stateBackgroundColor) {
        self.stateBackgroundColor = [NSMutableDictionary dictionary];
    }
    [self.stateBackgroundColor setObject:backgroundColor forKey:yypUserDisabled];
}
- (NSString *)yyp_stringForUIControlState:(UIControlState)state {
    NSString *yyp_string;
    switch (state) {
        case UIControlStateNormal:
            yyp_string = yypNormal;
            break;
        case UIControlStateHighlighted:
            yyp_string = yypHighlighted;
            break;
        case UIControlStateDisabled:
            yyp_string = yypDisabled;
            break;
        case UIControlStateSelected:
            yyp_string = yypSelected;
            break;
        default:
            yyp_string = yypNormal;
            break;
    }
    return yyp_string;
}
- (void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if (selected) {
        self.backgroundColor = (UIColor *)[self.stateBackgroundColor objectForKey:yypSelected];
    }else {
        self.backgroundColor = (UIColor *)[self.stateBackgroundColor objectForKey:yypNormal];
    }
}
- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    if (highlighted) {
        UIColor *color = (UIColor *)[self.stateBackgroundColor objectForKey:yypHighlighted];
        if (!color) {
            color = [UIColor lightGrayColor];
        }
        self.backgroundColor = color;
    }else {

    }
}
- (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled {
    [super setUserInteractionEnabled:userInteractionEnabled];
    if (!userInteractionEnabled) {
        self.backgroundColor = (UIColor *)[self.stateBackgroundColor objectForKey:yypUserDisabled];
    }else {
        self.backgroundColor = (UIColor *)[self.stateBackgroundColor objectForKey:yypNormal];
    }
}

@end

其中能解决我上述需求的其实是- (void)setUserInteractionDisabledBackgroundColor:(UIColor *)backgroundColor这行代码,我自己试了一下还蛮好用的,分享给大家,希望能对大家有所帮助。

你可能感兴趣的:(iOS UIButton按钮添加按状态设置背景色的方法)