iOS制作纯色UIImage

项目中用到,当按钮点下去时,设置背景颜色。可怜的是Apple iOS SDK只提供了
[[color=darkred]UIButton setBackgroundImage:pressedColorImg forState:UIControlStateHighlighted];[/color]

这个方法,下面这个方法没有提供不同状态的设置
[UIButton setBackgroundColor:];

只好想办法自己画UIImage,填充背景色
CGSize imageSize = CGSizeMake(50, 50);
UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
[[UIColor colorWithRed:0 green:0 blue:0 alpha:1.0] set];
UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

猜你喜欢

转载自muzilimeng06.iteye.com/blog/1765555