views:

118

answers:

2

How can I set the background color of a particular button in iPhone?

I used:

btnName1 = [ColorfulButton buttonWithType:UIButtonTypeRoundedRect];
btnName1.frame=CGRectMake(45,146,220,40);
[btnName1 setTitle:@"Continue" forState:UIControlStateNormal]; 
[btnName1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
[btnName1 setImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateNormal];
UIImage *img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"greenish" ofType:@"png"]];
UIImage *strechableImage = [img stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[btnName1 setBackgroundImage:strechableImage forState:UIControlStateNormal];
[btnName1 setNeedsDisplay];
[InfoView addSubview:btnName1];

It's correctly working on iPhone simulator but not on iPod (color not shown in iPod).

+2  A: 
myButton.backgroundColor = [UIColor redColor];

Or whatever colour you want. This method is inherited from UIView.

Note that this only sets the background colour...

If you want to change the button's look:

[myButton setBackgroundImage:[UIImage imageNamed:@"MyImage.png"] forState:UIControlStateNormal];

You can change the state to whichever state you like.

EDIT: If you are adding the button from interface builder, make sure to change the button Type to Custom, and change the image.

Chris Cooper
i used this eariler but it can't work on ipod.
suchita
@suchita does it work properly in simulator?
Warrior
Yes, could you elaborate on the problem? i think this should be doing it...
Chris Cooper
A: 

i had the solution when code is written in viewdidload() it's properly work eariler i used for uiview thatswhy it's not work.

suchita