UIImage + Color Category : Rex St John
One of the more irritating stumbling blocks I have found with UIKit controls is that many of them make use of a strange mixture of images and colors.
- ios
Originally published at rexstjohn.com on April 23, 2013. Recovered from the Internet Archive.
One of the more irritating stumbling blocks I have found with UIKit controls is that many of them make use of a strange mixture of images and colors. UISegmentedControl, for example, requires images for individual button segments and the dividers between the segments…when, in many cases, I find myself wanting to just make use of a simple color. The same is true of UIButton and elements of navigation bars.
I can’t remember where I found this, but it is a simple category which produces a 1×1 pixel image square in the desired color which may be used handily for such situations.
#import "UIImage+Color.h"
@implementation UIImage (Color)
+ (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0, 0, 1, 1);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[color setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end