8

I've recently been tearing my hair out over trying to set the same background color for views styled using Interface Builder, and views styled programatically using [UIColor colorWithRed: green: blue: alpha:]. Despite setting the same RGB values, the views styled in IB were a slightly different color shade to those styled in code.

I finally realised it was to do with color spaces - when I examined the views in IB using the color picker, it showed the color space was set to 'sRGB' rather than the default of 'Generic RGB' which UIColor uses. I never changed the color space setting myself. I then realised what is happening is that when you manually type in the RGB values in the color picker, it changes the color space to 'sRGB' without you knowing therefore giving a different color. However if you use the mouse to drag the slider bars to change the values then the color space remains at 'Generic RGB' and all is well.

Is this a bug, or am I misunderstanding the color picker? I'm using Xcode 6.1.

Jonathan Wareham
  • 3,357
  • 7
  • 46
  • 82
  • Hmmmh hand entered a blue value for a view that was slightly off the default blue we use normally and is in images we load dynamically....might be the same problem! – Lucas van Dongen Oct 30 '14 at 23:16
  • possible duplicate of [Wrong color in Interface Builder](http://stackoverflow.com/questions/14578759/wrong-color-in-interface-builder) – Matt Gibson Dec 09 '14 at 11:04

2 Answers2

2

A solution is to use "Device RGB" and not "Generic RGB". enter image description here

Scinfu
  • 1,081
  • 13
  • 18
1

I tried your 'technique' with only moving sliders, but in my case even this changes profile back to sRGB. I think its definitely a bug because the only 'workaround' to get exactly same colours in code and interface builder I found is the following:

  1. Decide on the RGB code you want to use (in Generic RGB) in my case was "#1C202C"

  2. Open new image in Photoshop in Generic RGB profile and fill the background with the color

  3. Go to menu Edit->Convert to Profile and convert to "sRGB IEC61966-2.1"

  4. Open Photoshop's color picker and pick on the new color, new rgb code will show up ... in my case "#252b3a"

If you use this code in interface builder then colors from code and IB will be exactly the same.

Matej Ukmar
  • 2,157
  • 22
  • 27