vijeesh
vijeesh

Reputation: 1325

Button background color is not showing properly

   [btn setBackgroundColor:[UIColor colorWithRed:199/255 green:57/255  blue:46/255  alpha:1]];

I tried this code to set button background color, but it is showing gray color,assigned color is something else

Upvotes: 0

Views: 655

Answers (3)

Shahzaib Maqbool
Shahzaib Maqbool

Reputation: 1487

you can try this solution..

[self.CompBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:118.0/255.0 green:183.0/255.0 blue:59.0/255.0 alpha:1]] forState:UIControlStateNormal];

i think this one helps you...

Upvotes: 1

luk2302
luk2302

Reputation: 57184

You have to change your arithmetics:

[btn setBackgroundColor:[UIColor colorWithRed:199.0/255.0 green:57.0/255.0  blue:46.0/255.0  alpha:1.0]];

What your code does is perform an integer division of 199 / 255 which results in something like 0.78 floating point but since you have not specified it to be a floating point arithmetic operation the program treats it as an integer division and omits the digits after the decimal point, resulting in 0. Same foes for the other 2 color components.

To make sure the operation is treated as floating point you should write 199.0 instead of 199 because that tells the compiler that you care about digits after the decimal point.

Strictly speaking it would be enough to only write either the divident or the divisor with .0. Both 199/255.0 and 199.0/255 will yield the correct result.

Upvotes: 3

Varun Naharia
Varun Naharia

Reputation: 5386

Try this

[btn setBackgroundColor:[UIColor colorWithRed:199.0/255.0 green:57.0/255.0  blue:46.0/255.0  alpha:1]];

Upvotes: 1

Related Questions