1. 程式人生 > >將UIButton 設定背景顏色 有點選highlight效果

將UIButton 設定背景顏色 有點選highlight效果

首先寫個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:

UIControlStateNormal];就有點選高亮的效果了