user1687717
user1687717

Reputation: 3475

ios: change button color

I changed a button color using the following code:

[self.testButton setBackgroundColor:[UIColor redColor]];

but I got a weird button:

enter image description here

why the button background color didn't change?

Upvotes: 2

Views: 8225

Answers (2)

Rose
Rose

Reputation: 437

Change the button type to Custom in Attributes inspector in your .xib then check your code

 [self.testButton setBackgroundColor:[UIColor redColor]]; 

Upvotes: 2

iDev
iDev

Reputation: 23278

You can import #import <QuartzCore/QuartzCore.h> and try this,

self.testButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.testButton.backgroundColor = [UIColor redColor];
self.testButton.layer.borderColor = [UIColor blackColor].CGColor;
self.testButton.layer.borderWidth = 0.5f;
self.testButton.layer.cornerRadius = 10.0f;

Or else you can use some custom image as the background image for this button.

Upvotes: 10

Related Questions