UIButton根据色值在不同状态的显示不同背景色

来源:互联网 发布:安康市江华软件 编辑:程序博客网 时间:2024/05/22 04:27
#import <UIKit/UIKit.h>@interface UIButton (KKFillColor)- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;@end

#import "UIButton+KKFillColor.h"@implementation UIButton (KKFillColor)- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state {    [self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];}+ (UIImage *)imageWithColor:(UIColor *)color {    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);    UIGraphicsBeginImageContext(rect.size);    CGContextRef context = UIGraphicsGetCurrentContext();        CGContextSetFillColorWithColor(context, [color CGColor]);    CGContextFillRect(context, rect);        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();        return image;}@end

0 0