SEG
SEG

Reputation: 1715

Customizing UIButton

I have seen in the in-built apps for iphone that there is a red delete UIButton. My question is, how do I change the color of the button. Usually there is a 'tint' attribute in the attributes inspector but there is not when using a UIbutton. Is there any way to programmatically change the color? I appreciate any help.

Upvotes: 3

Views: 4026

Answers (3)

roberto.buratti
roberto.buratti

Reputation: 2497

If your button is of type UIButtonTypeRoundedRect instead of UIButtonTypeCustom, setting the background color on the layer doesn't work.

Try this, it works for me.

UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.buttonAddToOrder.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor], (id)[[UIColor redColor] CGColor], nil];
gradient.cornerRadius = 10;

[myButton.layer insertSublayer:gradient atIndex:0];

Hope this may help.

Upvotes: 3

dasdom
dasdom

Reputation: 14073

You can make buttons with colors by using the layer of the button. First add the QuartzCore framework to your project. Then add #import <QuartzCore/QuartzCore.h> into your view class. To make a red button do this

UIButton *myButton = [UIButton buttonWithType: UIButtonTypeCustom];
[myButton setFrame: CGRectMake(10.0f, 10.0f, 300.0f, 44.0f)];
[[myButton layer] setBackgroundColor: [[UIColor redColor] CGColor]];
[self addSubview: myButton];
[myButton release];

Hope this helps!

Upvotes: 3

Joe
Joe

Reputation: 57179

That tint color for an UIButton is only available for the UIGlassButton which is an private undocumented API. Some solutions around using the private undocumented API are to use images or try drawing your own gradient.

Resources:

UIGlassButton in iPhone

http://cocoawithlove.com/2008/09/drawing-gloss-gradients-in-coregraphics.html

https://github.com/dermdaly/ButtonMaker

Upvotes: 3

Related Questions