Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

PaulieBoy

macrumors newbie
Original poster
Sep 14, 2008
3
0
I am trying to set the background color of a UIView to a custom color, without much success!

I can set it to a standard color like this...

Code:
[self setBackgroundColor:[UIColor lightGrayColor]];

But, when I try to set a custom color, it defaults to white!

The example below should be displayed as pale blue

Code:
[self setBackgroundColor:[[UIColor alloc] initWithRed:134 green:166 blue:228 alpha:1.0]];

I could add a blank image, with the custom color, and add that, but that is affecting some of my animations then...

Am I using the right method?

Any help would be really appreciated...
 

moopf

macrumors member
Aug 28, 2008
90
0
United Kingdom
OK, the value you're entering should be from 0 -> 1, e.g. 255 would be 1, 128 would be 0.5 etc. You can also just use:

Code:
[I][self setBackgroundColor:[[/I][I]UIColor colorWithRed[/I]:1.0 green:1.0 blue:1.0 alpha:1.0]];

Rather that using an init method.
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
Ah, this one caught me out too...setting RGB values on a 0..1 scale instead of the 8 bit scale you would usually use.

Here's a quick way of doing what you want:

Code:
// R: 128 G: 90 B: 200
UIColor *myColor = [UIColor colorWithRed:(128.0 / 255.0) green:(90.0 / 255.0) blue:(200.0 / 255.0) alpha: 1];
 

PaulieBoy

macrumors newbie
Original poster
Sep 14, 2008
3
0
Hey!

Thanks for the help...

Problem solved! We now have a pale blue background:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.