美文网首页iOSiOS新手学习
iOS将UIColor 转为UIImage 设置按钮背景颜色

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

作者: JasonEVA | 来源:发表于2016-07-13 15:57 被阅读597次

因为给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 设置按钮背景颜色

    本文链接:https://www.haomeiwen.com/subject/pyasjttx.html