Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#import <UIKit/UIKit.h>
@interface UIButton (UIButton_BackgroundImageAdditions)
-(void) setBackgroundImageWithColor: (UIColor *) color
cornerRadius: (CGFloat) cornerRadius
forState: (UIControlState) state;
@end
#import "UIButton+BackgroundImageAdditions.h"
@implementation UIButton (UIButton_BackgroundImageAdditions)
-(void) setBackgroundImageWithColor: (UIColor *) color
cornerRadius: (CGFloat) cornerRadius
forState: (UIControlState) state
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
UIBezierPath *roundedRectPath = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners: UIRectCornerAllCorners cornerRadii: CGSizeMake(cornerRadius, cornerRadius)];
CGContextAddPath( context, roundedRectPath.CGPath);
CGContextClip(context);
[color setFill];
UIRectFill(self.bounds);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGContextRestoreGState(context);
UIGraphicsEndImageContext();
[self setBackgroundImage: image forState: state];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment