How to change the toolbar color of the Navigation Controller in iOS?

27,547

Solution 1

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

Solution 2

Just do this:

navigationController.navigationBar.tintColor = [UIColor colorWithRed:117/255.0f green:4/255.0f blue:32/255.0f alpha:1];

Solution 3

You have to divide each value for 255. Try:

[UIColor colorWithRed:117/255.0f green:4/255.0f blue:32/255.0f alpha:1]

Solution 4

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);
Share:
27,547
aryaxt
Author by

aryaxt

Updated on August 18, 2020

Comments

  • aryaxt
    aryaxt over 3 years

    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];
    
  • aryaxt
    aryaxt over 12 years
    I did this and now it's showing up as black instead of dark red
  • TommyG
    TommyG over 12 years
    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.
  • aryaxt
    aryaxt over 12 years
    I don't think that's the case. because when i use [UIColor redColor], or any other defined color it works fine
  • TommyG
    TommyG over 12 years
    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
    TommyG over 12 years
    also - in which method are you calling this?
  • João Portela
    João Portela over 12 years
    those values should be floats and not integers, otherwise the result will be an integer.
  • Chris Nolet
    Chris Nolet about 12 years
    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
    Chris Nolet about 12 years
    @JoãoPortela: I've edited answer so that the values remain as floats.
  • Kevin Chen
    Kevin Chen over 10 years
    Note that it works differently on iOS 7: stackoverflow.com/a/18929980/1489823
  • zeroimpl
    zeroimpl over 9 years
    It is only that way in recent iOS, not when the question was asked.