Giving a button a color. As we saw, buttons have no setColor method, and setting the UIView background color is useless. Apple prefers you to use an image. Try this, Create an image and give it to the button. UIImage * ti = [UIImage imageNamed:@"blueButton.png"];
[button setBackgroundImage:ti forState:UIControlStateNormal];
Here is "blueButton.png". Download it, put it in the project folder and drag it into the list of resources in the project so it will be copied into the bundle. The image should appear in the button. If the button is wider than the image it will be stretched to fill the space. It looks ugly that way, so Apple has provided a "stretchable" image. That is created from an existing image by a method, UIImage * ti = [UIImage imageNamed:@"blueButton.png"];
ti = [ti stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[button setBackgroundImage:ti forState:UIControlStateNormal];
capWidth tells it how many pixels from the left side the image becomes stretchable in order to keep the left and right edges clean. Similarly for capHeight. See "Stretchable Views" in the View Programming Guide for iOS. |