简体   繁体   English

如何在iOS中更改导航控制器的工具栏颜色?

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

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. 以下rgb是深红色,但我的导航栏在以下代码后变为白色。

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 . 这是因为CGFloat值的范围是0.0 to 1.0而不是0 to 255 ,而高于1.0值被解释为1.0

Here is the documentation: UIColor 这是文档: UIColor

这样做:

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

您必须将每个值除以255.尝试:

[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. 我发现如果你来自网络或像Photoshop这样的东西,使用十六进制颜色更容易。 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). 真正的答案是.tintColor设置导航控制器导航项的颜色(如“完成”按钮)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM