故事板中的颜色与 UIColor 不匹配

我在代码中将颜色定义为

[UIColor colorWithHue:32.0/360.0 saturation:0.88 brightness:0.97 alpha:1]

如果我尝试在故事板中设置相同的颜色,当运行应用程序时,它是一个稍微不同的颜色在代码中定义的一个。如果我将颜色拖动到上颚,然后选择一个不同的颜色,再次选择上颚,HSB 值略有不同。它似乎是一个不同的(RGB?)Interface Builder 的颜色。

18496 次浏览

Seems this is a bug. When logging the color set by IB in code, it has the values H:27.982594 S:0.899257 B:0.953253

I have filed a bug report to Apple: rdar://19744216

Thanks to Zaph

To log:

CGFloat cols[4];
[color getHue:&cols[0] saturation:&cols[1] brightness:&cols[2] alpha:&cols[3]];
NSLog(@"H:%f S:%f B:%f %f",cols[0]*360, cols[1], cols[2], cols[3]);

I had the same issue. I was seeing runtime RGB values of the colors from storyboards not matching UIColors created at runtime in code. I was able to fix this in storyboards by setting the color to be "Generic RGB" (vs the default of sRGB) when configuring it. Here is a screenshot of what I'm talking about in IB:

IB Color config util

Xcode 8+, iOS 10+

I recently faced this problem and none of the posted answers did it. It turns out that with the release of iOS 10 SDK, the UIColor initializer init(red:green:blue:alpha:) now uses the extended sRGB range, so you have to set accordingly when configuring your color values on the Storyboard.

enter image description here

See Apple's documentation: https://developer.apple.com/reference/uikit/uicolor/1621925-init

Swift 3

In my case what was exactly accurate was Color LCD:

enter image description here

I hope I've helped :-D

With code, UIColor init(red:green:blue:alpha:) method return color object with sRGB color space; With xib / StoryBoard, we need to select "sRGB IEC..." if we want to get same appearance as using code.

Such as:

UIColor.init(red: (20.0/255.0), green: (20.0/255.0), blue: (20.0/255.0), alpha: 1.0)

xib / StoryBoard screenshot

Actually,this problem has nothing to do with iOS 10. After iOS 10, UIColor init uses "extended sRGB" instead of sRGB. And this will causes problems only when we use r g b values below 0.0 and above 1.0.

iOS 10 UIColor related

Following are 3 lines to achieve the desired navigation bar background color:

navigationBar.isTranslucent = false
navigationBar.backgroundColor = UIColor.black
navigationBar.barTintColor = UIColor.black