Question

How can I change the default color of the button on iPhone?

Was it helpful?

Solution

A common technique is to use an image for the background. Actually you can use a stretchable image:

- (void)viewDidLoad {
    UIImage *blueImage = [UIImage imageNamed:@"blueButton.png"];
    UIImage *blueButtonImage = [blueImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [myButton setBackgroundImage:blueButtonImage forState:UIControlStateHighlighted];
}

The original image:

alt text

The end result:

alt text

OTHER TIPS

I came across this post that explains how to do this without images and completely in code.

http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

Hope that helps.

Assuming you mean UIButton, you can only change the title color using setTitleColor:forState:.

You can't change the background color or the shape. You have to create your own control for that.

I started this thread on iphonedevsdk.com.

Hopefully we'll get an answer there if not here.

A great way to customize UIButtons is by directly manipulating the Core Animation Layers. You can find a good tutorial on "Cocoa is My Girlfriend"

http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top