Kyle Griffith
Kyle Griffith

Reputation: 114

customly set button background color ios

I am trying to change the background color of UIButton to a custom color. All the default greycolor, bluecolor, etc do not suffice.

    UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
    _button.backgroundColor = [UIColor myColor]; 

It gives error on 2nd line saying

No known class method for selector 'myColor'

Upvotes: 0

Views: 409

Answers (4)

user6039929
user6039929

Reputation:

It's much more convenient to use:

 _button.backgroundColor = [UIColor colorWithRed:1.0/255.0 green:1.0/255.0 blue:0.0/255.0 alpha:1.0f];

Upvotes: 0

Rashad
Rashad

Reputation: 11197

Try this:

 UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
 _button.backgroundColor = myColor;

myColor is not defined in UIColor. It's a variable that you defined.

Upvotes: 0

Bhavin Bhadani
Bhavin Bhadani

Reputation: 22374

just change your current code with below code

UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];
_button.backgroundColor = myColor;  // because myColor is UIColor

Upvotes: 1

Anbu.Karthik
Anbu.Karthik

Reputation: 82756

your code is fine, the simple mistake is again you creted the [UIColor property] on second line

UIColor *myColor=[UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f];

Not like

  _button.backgroundColor = [UIColor myColor]; 

Do like

  _button.backgroundColor = myColor; 

Update

you can directly use the color property like

   _button.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0 alpha:1.0f]; 

Upvotes: 1

Related Questions