76

I am defining a color in code as

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

If I try to set the same color in storyboard, when running the App it is a slightly different colour to the one defined in code. If I drag the color to the palate then select a different color and select the palate one again, the HSB values are slightly different. It seems it is snapping to a different (RGB?) color in interface builder.

Nick
  • 3,958
  • 4
  • 32
  • 47
  • Using `colorWithHue` and not RGB web hex values: great choice. – zaph Feb 06 '15 at 14:24
  • 3
    In your code try logging the HSB values set by IB. If they are different file a bug: bugreporter.apple.com. I think I have also seen this issue. – zaph Feb 06 '15 at 14:26
  • same happened with me when selecting a UIView's bg color and setting same bg color for UITableView but different color. To achieve that i had make changes in color's alpha – Paresh Navadiya Feb 06 '15 at 14:31
  • Storyboard color logged: H:27.982594 S:0.899257 B:0.953253 I will report – Nick Feb 06 '15 at 14:41

6 Answers6

228

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

camilomq
  • 3,021
  • 1
  • 17
  • 10
  • Thanks for the heads up! Is it possible to apply this change globally across every Interface Builder (.xib and .storyboard) file without running some sort of manual script? – Josh Sklar Aug 05 '18 at 13:48
  • I think this gives a very good explanation about what you should do: https://lembergsolutions.com/blog/how-get-right-color-ios-detailed-instruction – Warpzit May 20 '19 at 11:12
  • Thank you! This helps! – Dmytro Yashchenko Mar 18 '21 at 18:07
40

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

mattr
  • 5,458
  • 2
  • 27
  • 22
  • I think I had tried that before, but it does seem to keep the correct values with Generic RGB now in Xcode 6.2, thanks – Nick Mar 23 '15 at 23:38
  • hey @Nick. i am also on Xcode 6.2 and had that same problem you describe where it doesnt keep the values you set. You have to play with it a little; there are conditions that cause it to reset (its a little buggy but i was able to work around them). – mattr Mar 24 '15 at 00:53
  • Please see @Camilo M answer if you're on ios 10 xcode 8 – Le Duc Duy Oct 15 '16 at 05:22
8

Swift 3

In my case what was exactly accurate was Color LCD:

enter image description here

I hope I've helped :-D

3

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

Jirui
  • 139
  • 1
  • 6
1

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]);
Nick
  • 3,958
  • 4
  • 32
  • 47
0

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

navigationBar.isTranslucent = false
navigationBar.backgroundColor = UIColor.black
navigationBar.barTintColor = UIColor.black
Alex
  • 448
  • 7
  • 14