从故事板设置的UINavigationBar背景颜色与代码设置的颜色不同 [英] UINavigationBar background color setted from storyboard differ from the color setted by code

查看:174
本文介绍了从故事板设置的UINavigationBar背景颜色与代码设置的颜色不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用仅适用于 iOS 8 ,我正在使用 XCode 6.1.1

My app will be available only for iOS 8 and I'm working with XCode 6.1.1.

如果我通过故事板设置颜色(在导航栏上设置 Bar Tint 属性 section)

If I set the color through the storyboard (setting theBar Tint attribute on Navigation Bar section)

所需颜色为:


56 186 145

56 186 145

我用核心编码实用程序,以获取我的颜色的浮动值。

I used Core coding utilities to get the floating values of my color.

按代码:

let backgroundColor = UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.translucent = false

通过故事板设置的颜色与原始RGB相同。

Color setted through storyboard is the same as the Original RGB.


  • 按代码:

  • 按故事板:

编辑

我尝试使用让storyColor = UIColor(红色:0.255,绿色:0.769,蓝色:0.635,alpha:1.0)和故事板中的rgb: 65 196 162 基于@siejkowski评论,但我得到以下颜色:

I tried using let backgroundColor = UIColor(red: 0.255, green: 0.769, blue: 0.635, alpha: 1.0) and rgb from storyboard: 65 196 162 based on @siejkowski comment, but I get these colors:


  • 按代码:

  • By code:

通过故事板:

By storyboard:

为什么?

推荐答案

你观察到颜色差异的两个原因。

There is two reason for the color differences here you observed.


  1. 故事板中设置的值是 RGB(sRGB IEC61966-2.1)类型,当您按RGB值编码 UIColor 时它将返回 RGB(通用RGB)值。

  1. The value set in Storyboard is RGB (sRGB IEC61966-2.1) type and when you code UIColor by RGB value it will return RGB (Generic RGB) values.

因此当您从Storyboard更改颜色时,值为不同的RBG类型。在Storyboard中获取 RGB滑块的精确RGB值更改类型。

So when you change color from Storyboard the values are different for RBG types. To get exact RGB value change type of RGB Sliders in Storyboard.

单击 RGB滑块完全正确的设置图标选项。它将显示一个弹出菜单 - 选择 Generic RGB 选项。

Click on Settings icon which is exactly right to the RGB Sliders Option. It will show a pop-up menu - select Generic RGB option.

现在您可以观察到RGB值的图像

Now you can observe an image that values for RGB

56 186 145

现在更改为

49 175 126

所以这些是代码的期望值。

So these are desire values to code.


  1. 综述问题:

  1. Roundup issue:

在代码中,您传递给出参数的向上舍入值,如下线所示

In code, you are passing giving round up values for parameter like in below line

UIColor(red: 0.22, green: 0.729, blue: 0.569, alpha: 1.0)

所以每个像素会做一个小改动的颜色代码。我建议你将这个值除以 255 ,然后对编译器进行向上计算。这将为您提供所需的颜色准确度。

So it will make a small change per pixel of the color code. I suggest you divide this values by 255 and leave round up calculation for the compiler. Which will gives you desire color accuracy.

现在,对于新值,更新代码将是:

So now for new values update code will be:

let backgroundColor = UIColor(red: 49.0/255.0, green: 175.0/255.0, blue: 126.0/255.0, alpha: 1.0)
self.navigationController!.navigationBar.barTintColor = backgroundColor
self.navigationController!.navigationBar.isTranslucent = false

这篇关于从故事板设置的UINavigationBar背景颜色与代码设置的颜色不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆