- 作項目常常遇到一個問題,給Button增長一個高亮狀態,若是公司有ui還好,能夠讓ui給你切個高度狀態的圖片,沒有ui只能本身切圖或給按鈕添加背景顏色,可是這個處理起來有沒麻煩,而且一個項目不過能只有那麼一個button須要添加這個狀態顏色,因此本身也常試封裝一個button的背景顏色,上代碼,若是哪位大神有好的建議也但願能告訴我,直接留言,謝謝
//這是繼承UIButton的擴展類
//UIButton+MHFillColor.h
@interface UIButton (MHFillColor)
- (void)ym_setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state;
@end
複製代碼
//UIButton+MHFillColor.m
@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
複製代碼