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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7179741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:15:19  来源:igfitidea点击:

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

iphoneiosuinavigationcontrolleruinavigationbar

提问by aryaxt

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

回答by Oscar Gomez

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

这是因为CGFloat值的范围从0.0 to 1.0not from 0 to 255,并且上面1.0的值被解释为1.0

Here is the documentation:UIColor

这是文档:UIColor

回答by TommyG

Just do this:

只需这样做:

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

回答by fncap

You have to divide each value for 255. Try:

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

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

回答by monzonj

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:

我发现如果您来自网络或 Photoshop 之类的软件,使用十六进制颜色会更容易。你可以使用这个宏:

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

回答by broken_image

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 为导航控制器的导航项(如“完成”按钮)设置颜色。