Custom background color on a UIView...

Discussion in 'iOS Programming' started by PaulieBoy, Sep 16, 2008.

  1. PaulieBoy macrumors newbie

    Sep 14, 2008
    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...

    [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

    [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...
  2. moopf macrumors member

    Aug 28, 2008
    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:

    [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.
  3. Luke Redpath macrumors 6502a

    Nov 9, 2007
    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:

    // 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];
  4. PaulieBoy thread starter macrumors newbie

    Sep 14, 2008

    Thanks for the help...

    Problem solved! We now have a pale blue background:)

Share This Page