Programatically adding a iOS 6 look alike gradient UIButton

65 Views Asked by At

I am trying to draw a red button programmatically and want look and feel exactly that for an iOS6 button (including gradient effect).

I am doing this to support one of my application on iOS 6. I tried with custom button but no able to reach the milestone. Appreciate any quick help.

Here is what I have done in my custom 'UITableViewCell' class:

UIButton *myRedButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.contentView.bounds) - 100, 0.0, 100, 35)];
myRedButton.titleLabel.font = [UIFont systemFontOfSize:12];
myRedButton.backgroundColor = [UIColor colorWithRed:194.0/255.0 green:21.0/255.0 blue:35.0/255.0 alpha:1.0];
myRedButton.layer.cornerRadius = 8.0;
myRedButton.clipsToBounds = YES;
[self.contentView addSubview:myRedButton];

enter image description here

1

There are 1 best solutions below

2
Abhinav On

For the benefit of others (in case some one is looking around for similar context) here is how I achieved it:

UIButton *myRedButton = [[UIButton alloc] initWithFrame:autoReverseButtonFrame];
myRedButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
myRedButton.layer.cornerRadius = 8.0;
myRedButton.clipsToBounds = YES;
myRedButton.layer.borderColor = [UIColor blackColor].CGColor;
self.autoReverseButton.layer.borderWidth = 1.0;

CGColorRef topColor = [UIColor colorWithRed:240.0/255.0 green:127.0/255.0 blue:133.0/255.0 alpha:1.0].CGColor;
CGColorRef bottomColor = [UIColor colorWithRed:194.0/255.0 green:21.0/255.0 blue:35.0/255.0 alpha:1.0].CGColor;

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = myRedButton.bounds;
gradient.cornerRadius = myRedButton.layer.cornerRadius;
gradient.colors = @[(__bridge id)topColor, (__bridge id)bottomColor];
[myRedButton.layer insertSublayer:gradient atIndex:0];