开发者

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

开发者 https://www.devze.com 2023-03-30 17:42 出处:网络
I am trying开发者_StackOverflow中文版 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.

I am trying开发者_StackOverflow中文版 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];


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


Just do this:

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


You have to divide each value for 255. Try:

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


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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消