Skip to content

an Objective-c Category to add the method setBackgroundColor:forState: to the UIButton

Notifications You must be signed in to change notification settings

damienromito/UIButton-setBackgroundColor-forState-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

UIButton setBackgroundColor:forState:

An Objective-c Category to add the method setBackgroundColor:forState: to the UIButton

UIButton+BackgroundColor.h

@interface UIButton(BackgroundColor)
- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state;
@end

UIButton+BackgroundColor.m

#import "UIButton+BackgroundColor.h"

@implementation UIButton(BackgroundColor)

- (void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state
{
    [self setBackgroundImage:[self imageWithColor:color] forState:state];
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end

About

an Objective-c Category to add the method setBackgroundColor:forState: to the UIButton

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published