自定义Button高亮状态

  • 作项目常常遇到一个问题,给Button增长一个高亮状态,若是公司有ui还好,能够让ui给你切个高度状态的图片,没有ui只能本身切图或给按钮添加背景颜色,可是这个处理起来有没麻烦,而且一个项目不过能只有那么一个button须要添加这个状态颜色,因此本身也常试封装一个button的背景颜色,上代码,若是哪位大神有好的建议也但愿能告诉我,直接留言,谢谢

//这是继承UIButton的扩展类
//UIButton+MHFillColor.h
#import <UIKit/UIKit.h>
 
@interface UIButton (MHFillColor)
 
- (void)ym_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;
 
@end
复制代码

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

复制代码
相关文章
相关标签/搜索