將UIButton 設定背景顏色 有點選highlight效果
阿新 • • 發佈:2019-02-12
首先寫個UIImage的類別 為的就是要點選效果。PS:直接 [self.rightBtn setBackgroundColor:color]是沒有點選高亮效果的。
@implementation UIImage (colorful)
+ (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
然後
[self.rightBtnsetBackgroundImage:[UIImageimageWithColor:[UIColorcolor]] forState: