Skip to content

Instantly share code, notes, and snippets.

@rcdilorenzo
Created October 14, 2013 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcdilorenzo/6979461 to your computer and use it in GitHub Desktop.
Save rcdilorenzo/6979461 to your computer and use it in GitHub Desktop.
#import <UIKit/UIKit.h>
@interface SODimButton : UIButton
@property (nonatomic, strong) UIColor *buttonColor;
@property (nonatomic, strong) UIColor *selectedButtonColor;
@property (nonatomic) CGFloat backgroundAlpha;
@end
#import "SODimButton.h"
@implementation SODimButton
- (void)drawRect:(CGRect)rect {
// More drawing can be done if you get the context.
// I actually don't use it in this example.
CGContextRef context = UIGraphicsGetCurrentContext();
if (self.selected || self.highlighted) {
[[self.selectedButtonColor colorWithAlphaComponent:self.backgroundAlpha] setFill];
} else {
[[self.buttonColor colorWithAlphaComponent:self.backgroundAlpha] setFill];
}
UIRectFill(rect);
}
- (void)setSelected:(BOOL)selected {
[super setSelected:selected];
[self setNeedsDisplay];
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
SODimButton *button = [[SODimButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 100, 50, 200, 44)];
[button setTitle:@"My Button" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.buttonColor = [UIColor redColor];
button.selectedButtonColor = [UIColor colorWithRed:0.73f green:0.10f blue:0.00f alpha:1.00f];
button.backgroundAlpha = 0.2f;
[self.view addSubview:button];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment