16

I am trying to change the color of my navigation bar. The following rgb is for a dark red color, but my nav bar turns white after the following code.

navigationController.navigationBar.tintColor = [UIColor colorWithRed:117 green:4 blue:32 alpha:1];
halfer
  • 19,824
  • 17
  • 99
  • 186
aryaxt
  • 76,198
  • 92
  • 293
  • 442

5 Answers5

28

This is because the CGFloat values range from 0.0 to 1.0 not from 0 to 255, and values above 1.0 are interpreted as 1.0.

Here is the documentation:UIColor

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
16

Just do this:

navigationController.navigationBar.tintColor = [UIColor colorWithRed:117/255.0f green:4/255.0f blue:32/255.0f alpha:1];
Chris Nolet
  • 8,714
  • 7
  • 67
  • 92
TommyG
  • 4,145
  • 10
  • 42
  • 66
  • I did this and now it's showing up as black instead of dark red – aryaxt Aug 24 '11 at 17:55
  • Looks like you are seeing either only white or only black for all the options...are you sure you dont have a B&W monitor? :) make sure you are not overriding this tint color somewhere else in your app. – TommyG Aug 24 '11 at 18:13
  • I don't think that's the case. because when i use [UIColor redColor], or any other defined color it works fine – aryaxt Aug 24 '11 at 20:14
  • try something like that: UIColor *color = [UIColor colorWithRed:117/255.f green:4/255.f blue:32/255.f alpha:1]; navigationController.navigationBar.tintColor = color; – TommyG Aug 24 '11 at 20:18
  • also - in which method are you calling this? – TommyG Aug 24 '11 at 20:19
  • 2
    Haha, that's because it's converting the fractions to integers :) You need to specify fractions like this: `117/255.0f` That way they stay as floats. – Chris Nolet Mar 25 '12 at 02:23
  • Note that it works differently on iOS 7: http://stackoverflow.com/a/18929980/1489823 – Kevin Chen Jan 03 '14 at 05:28
6

You have to divide each value for 255. Try:

[UIColor colorWithRed:117/255.0f green:4/255.0f blue:32/255.0f alpha:1]
M Jesse
  • 2,213
  • 6
  • 31
  • 37
fncap
  • 126
  • 5
3

I find that if you come from the web or from something like Photoshop, it is easier to work with Hexadecimal colors. You can use this macro for that:

//RGB color macro
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

And use it like this:

self.navigationBar.tintColor = UIColorFromRGB(0xd8dadf);
monzonj
  • 3,659
  • 2
  • 32
  • 27
0

Ah, this is funny. The real answer is that .tintColor sets the color for the navigation controller's navigation Items (like a "Done" button).

broken_image
  • 13
  • 1
  • 1