iOS将UIColor 转为UIImage 设置按钮背景颜色

因为给UIButton设置backgroundImag可以使其在被点击的时候自动产生点击效果,backgroundColor并没有这个效果。然而大多数时候我们需要给按钮设置的背景都为纯色,所以特意写了个将纯色转换为Image的方法,方便使用。


/**

* 创建纯色的图片,用来做背景

*/

+ (UIImage *)switchToImageWithColor:(UIColor *)color size:(CGSize)size

{

UIGraphicsBeginImageContextWithOptions(size, 0, [UIScreen mainScreen].scale);

[color set];

UIRectFill(CGRectMake(0, 0, size.width, size.height));

UIImage *ColorImg = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return ColorImg;

}



你可能感兴趣的:(iOS将UIColor 转为UIImage 设置按钮背景颜色)