Closed tsilva closed 9 years ago
Buttons only support [button setBackgroundImage:forState:]. The following category method can be added to UIButton to solve this:
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state { UIImage *image = [UIImage imageWithColor:backgroundColor]; [self setBackgroundImage:image forState:state]; }
And the following to UIImage:
+ (UIImage *)imageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0, 0.0, 1.0, 1.0); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
Description
Buttons only support [button setBackgroundImage:forState:]. The following category method can be added to UIButton to solve this:
And the following to UIImage: