xcode UINavigationBar 背景颜色不是我设置的确切 UIColor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22844253/
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
UINavigationBar background color not the exact UIColor I set it to
提问by ?zg
Please look at the screenshot below.
请看下面的截图。
I set all the tint colors from the same UIColor object for the UINavigationBar, search bar and toolbar at the bottom. But for some reason the navigationbar color is slightly different. What could be the reason for this?
我为底部的 UINavigationBar、搜索栏和工具栏设置了来自同一个 UIColor 对象的所有色调颜色。但由于某种原因,导航栏颜色略有不同。这可能是什么原因?
Thanks..
谢谢..
回答by Danyun Liu
The difference between your navigation bar and search bar is because the navigation bar is translucent,the system will apply a default alpha value for the navigation bar.
你的导航栏和搜索栏的区别是因为导航栏是半透明的,系统会为导航栏应用一个默认的alpha值。
Try self.navigationController.navigationBar.translucent = NO;
尝试 self.navigationController.navigationBar.translucent = NO;
回答by Brandon A
UINavigationBar
has a bit of a strange behavior when it comes to the color you set on it. As other members have pointed out, you do have to set the translucent
boolean to false
, but this isn't all to get your bar to match the color you are trying to set. It will be close, but if you look carefully it will not be the exact color you are trying to use. In order to set the truecolor on UINavigationBar
you need to understand what it is doing.
UINavigationBar
当涉及到您设置的颜色时,它有一些奇怪的行为。正如其他成员指出的那样,您必须将translucent
布尔值设置为false
,但这并不是让您的条形与您尝试设置的颜色相匹配的全部。它会很接近,但如果仔细观察,它不会是您尝试使用的确切颜色。为了设置真实的颜色,UINavigationBar
您需要了解它在做什么。
Let's say that I wanted to set my UINavigationBar
to this cool color green.
假设我想将我的设置UINavigationBar
为这种很酷的绿色。
That would be an RGB value of: R=90 | G=200 | B=95.
这将是一个 RGB 值:R=90 | G=200 | 乙=95。
What UINavigationBar
will do is apply it's built-in styling by giving this green a "glossy" look. The result for us is that it is taking our green RGB values and uppingeach by a factor of 20.
什么UINavigationBar
都会做的是运用它的内置给予这个绿色的“有光”的外观造型。我们的结果是它采用了我们的绿色 RGB 值并将每个值增加了 20 倍。
If you look close the green square above does not exactly match the one UINavigationBar
is displaying with the same RGB values. This looks just slightly brighter.
如果您仔细观察,上面的绿色方块与UINavigationBar
显示的具有相同 RGB 值的方块不完全匹配。这看起来只是稍微亮一点。
To fix this, simply reduce the RBG values by 20 for the color you intend to use for the UINavigationBar
's in your application.
要解决此问题,只需将要用于UINavigationBar
应用程序中 ' 的颜色的 RBG 值减少 20 。
So R=90 | G=200 | B=95 will become R=70 | G=180 | B=75.
所以 R=90 | G=200 | B=95 会变成 R=70 | G=180 | 乙=75。
回答by Harshad Madaye
You will have to disable property translucentfor navigation bar
您将不得不禁用导航栏的属性半透明
you can do it programatically with following line
您可以使用以下行以编程方式执行此操作
self.navigationController?.navigationBar.isTranslucent = false
OR
或者
Just un-tick "Translucent" check box from storyboard, you will find it in UINavigationBar properties on UINavigationController
只需取消故事板中的“半透明”复选框,您就会在 UINavigationController 的 UINavigationBar 属性中找到它
回答by Krivoblotsky
It differs because of default UINavigationBar blur effect. I can suggest to set 2px width stretchable background image with your color. Something like this:
它因默认的 UINavigationBar 模糊效果而不同。我可以建议使用您的颜色设置 2px 宽度的可拉伸背景图像。像这样的东西:
UIImage *image = [UIImage imageNamed:@"yourBlueImage"];
image = [image stretchableImageWithLeftCapWidth:image.size.width / 2.0f topCapHeight:0.0f];
[yourNavBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
It's like a trick, because when you set custom backgroundImage blur effect will be disabled and you'll get exact color.
这就像一个技巧,因为当您设置自定义背景图像时,模糊效果将被禁用,您将获得准确的颜色。
回答by atraczyk
navigationBar.barStyle = UIBarStyle.blackOpaque
Worked to prevent undesired blending with the background, when none of the previous answers worked (in iOS 9.3).
当之前的答案都不起作用时(在 iOS 9.3 中),努力防止与背景发生不希望的混合。
回答by Yvonne Marggraf
I've tried every solution, mentioned in the answers here. But nothing solved it completly. I found this:
我已经尝试了这里的答案中提到的所有解决方案。但没有什么能完全解决它。我找到了这个:
First of all you have to have to select the navigationbar and disable the Translucent Checkbox. It's right at the top.
首先,您必须选择导航栏并禁用半透明复选框。它就在顶部。
After this is done. The BackgroundColor is still wrong. So don't set the BackgroundColor, set the TintColor of the NavBar. You can do this on the same way like the Translucent. The possibility to set the TintColor is 2 elements under the Translucent Checkbox.
完成此操作后。BackgroundColor 仍然是错误的。所以不要设置BackgroundColor,设置NavBar的TintColor。您可以使用与半透明相同的方式来执行此操作。设置 TintColor 的可能性是半透明复选框下的 2 个元素。
Or you set it using swift or objC:
或者您使用 swift 或 objC 设置它:
self.navigationController?.navigationBar.isTranslucent = false
navBar.barTintColor = UIColor.CustomColor.colorPrimary
回答by abhi
Try this, this works
试试这个,这有效
navigationController?.navigationBar.shadowImage = UIImage() navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage() navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
回答by Tomas
I had the same problem as the op. After number of hours of failed attempts I decided to change the logic in my code:
我和 op 有同样的问题。经过数小时的失败尝试后,我决定更改代码中的逻辑:
First, set the backround color of the navigation bar:
首先,设置导航栏的背景颜色:
navigationBar.barTintColor = UIColor.red
iOS will make the red a bit darker/lighter as described by others in this thread.
正如本线程中的其他人所描述的那样,iOS 会使红色更暗/更亮。
Second, take the color of the navigation bar and set it to what you need, in my case the background of the cell:
其次,获取导航栏的颜色并将其设置为您需要的颜色,在我的例子中是单元格的背景:
cell.backgroundColor = navigationBar.barTintColor
Now the colors will be the same.
现在颜色将相同。
回答by Abhijit Hadkar
回答by serg_zhd
FOR FURTHER READERS
给更多读者
If none of solutions worked for you try to check this one.
如果没有任何解决方案适合您,请尝试检查此解决方案。
Probably you have set the background color of your view controller in the Storyboard using Generic RGB which differs from sRGB used in UIColor class since iOS 10 SDK.
可能您已经使用 Generic RGB 在 Storyboard 中设置了视图控制器的背景颜色,这与自 iOS 10 SDK 以来 UIColor 类中使用的 sRGB 不同。