开发者

Customizing UIButton

开发者 https://www.devze.com 2023-02-27 10:50 出处:网络
I have seen in the in-built apps for iphone that there is a red delete UI开发者_如何转开发Button. My question is, how do I change the color of the button. Usually there is a \'tint\' attribute in the

I have seen in the in-built apps for iphone that there is a red delete UI开发者_如何转开发Button. 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.


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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消