TeenDev13
TeenDev13

Reputation: 189

How to Change the Color of a UIButton?

I'm in XCode right now and I was wondering how I could change the color of my UIButton. I've dragged the button from the Storyboard to my .m file and tried to write some code underneathe it, but it didn't work properly. Also, will I have to place the same code in my header file. I know it's simpler than what I'm doing. Any suggestions?

- (IBAction)OrBoton:(id)sender 

{
   UIButton *button = (UIButton *)sender;
   [button setBackgroundColor:[UIColor redColor]];
}

Upvotes: 0

Views: 7177

Answers (1)

Lithu T.V
Lithu T.V

Reputation: 20021

Well about button background color..

If button type rounded rect ..Color cannot be applied. So set the button to type custom and good to go

 self.myButton.backgroundColor = [UIColor redColor];

or you can set the background image or image to the button to gain the color

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

or

[self.myButton setImage:[UIImage imageNamed:@"myButtonImage.png"] forState:UIControlStateNormal];

Upvotes: 2

Related Questions