//根据颜色创建一个图片
+ (UIImage *)createImageWithColor:(UIColor *)color rect:(CGRect)rect
{
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
然后就可以调用这个方法创建了一个UIImage的对象,接着就可以直接调用系统的- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state 这个方法设置默认和高亮的颜色了,很方便。