Configure UIButton

I saw in the embedded applications for the iphone that there is a red UIButton removal. My question is how to change the color of a button. Typically, the attribute inspector has a tint attribute, but not with UIbutton. Is there a way to programmatically change the color? I appreciate any help.

+4
source share
3 answers

You can create buttons with colors using a button layer. First add the QuartzCore framework project to the project. Then add #import <QuartzCore/QuartzCore.h> to the view class. To make a red button, do it

 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!

+3
source

This hue color for UIButton is only available for UIGlassButton , which is a private undocumented API. Some solutions for using the undocumented private API are using images or trying to draw your own gradient .

Resources

UIGlassButton on iPhone

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

https://github.com/dermdaly/ButtonMaker

+3
source

If your button is of type UIButtonTypeRoundedRect instead of UIButtonTypeCustom , setting the background color on the layer does not work.

Try it, 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 helps.

+3
source

All Articles