简体   繁体   English

为什么IB中定义的颜色与代码中定义的颜色不同?

[英]Why is color defined in IB different than one defined in code?

I have a single view app. 我有一个单视图应用程序。 I define two UIView instances inside that single view. 我在单个视图中定义了两个UIView实例。 I want both to have same color. 我希望两者都具有相同的颜色。

For first view I define it in Interface Builder and pick a color. 对于第一个视图,我在Interface Builder中定义它并选择一种颜色。 I want #999999 (grey). 我想要#999999 (灰色)。 New color picker has possibility to enter hex color values so it is easy to enter: 999999 . 新颜色选择器可以输入十六进制颜色值,因此很容易输入: 999999

For the second view, I define it by setting background color of second view. 对于第二个视图,我通过设置第二个视图的背景色来定义它。 I set same #999999 color using [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1] . 我使用[UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1]设置了相同的#999999颜色。

Simple calculation: #99 -> 153 -> 153/255 == 0.6. 简单计算: #99 -> 153 -> 153/255 == 0.6. So far good. 到目前为止很好。

However resulting colors are different. 但是,产生的颜色是不同的。 Why? 为什么? This feels like a bug in xcode. 感觉就像xcode中的错误。 After debugging resulting colors: 调试结果颜色后:

Color defined in IB: UIDeviceRGBColorSpace 0.529648 0.529632 0.529641 1 IB中定义的颜色: UIDeviceRGBColorSpace 0.529648 0.529632 0.529641 1

Color defined in code: UIDeviceRGBColorSpace 0.6 0.6 0.6 1 代码中定义的颜色: UIDeviceRGBColorSpace 0.6 0.6 0.6 1


UPDATE: I know about "duplicates" to this question. 更新:我知道这个问题的“重复”。 They have one thing in common. 他们有一个共同点。 They properly identified the problem but failed miserably to solve it. 他们正确地发现了问题,但未能解决。 It tested developer color pickers from Panic and Skala. 它测试了Panic和Skala的开发人员颜色选择器。 They have same problem. 他们有同样的问题。 And as insult to injury they offer copy color as hex values. 而且,由于受伤,他们提供了十六进制值作为副本颜色。 Guest what, numbers there are not affected by color profile at all. 来宾什么,那里的数字根本不受颜色配置文件的影响。 So for example color #999 they provide exactly 0.6 for each color component. 因此,例如颜色#999它们为每种颜色分量提供正好0.6 Unfortunately, if color is used directly it has color profile attached and resulting color is different. 不幸的是,如果直接使用颜色,则会附加颜色配置文件,从而导致颜色不同。 Only way to get exact RBG color is to use "safe web colors" where resulting color is with generic RGB color profile, in other words not affected. 获得精确RBG颜色的唯一方法是使用“安全网页颜色”,其中所得颜色具有通用RGB颜色配置文件,换句话说,不受影响。 Problem is that safe web colors doesn't contain all colors our designers use. 问题是安全的网页颜色并不包含我们的设计师使用的所有颜色。

IB中定义

Here is a category on UIColor that can create a UIColor from a hex string if it will help. 这是UIColor上的类别,如果有帮助,可以从十六进制字符串创建UIColor

+ (UIColor *)rz_colorFromHex:(uint32_t)hexLiteral
{
    uint8_t r = (uint8_t)(hexLiteral >> 16);
    uint8_t g = (uint8_t)(hexLiteral >> 8);
    uint8_t b = (uint8_t)hexLiteral;

    return [self rz_colorFrom8BitRed:r green:g blue:b];
}

+ (UIColor *)rz_colorFromHexString:(NSString *)string
{
    NSParameterAssert(string);
    if ( string == nil ) {
        return nil;
    }

    unsigned int hexInteger = 0;
    NSScanner *scanner = [NSScanner scannerWithString:string];

    [scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:@"x#"]];
    [scanner scanHexInt:&hexInteger];

    return [self rz_colorFromHex:hexInteger];
}

As to why your colors are different. 至于为什么你的颜色不同。 I would set a breakpoint and see that the colors are in fact the same while it is running. 我将设置一个断点,并在运行时看到颜色实际上是相同的。 You can even print out the rgb channels for each of the colors to make sure they aren't different. 您甚至可以为每种颜色打印出rgb通道,以确保它们没有不同。

I would be surprised if it is an Xcode bug, but let me know what happens when you inspect the colors from the debugger. 如果它是Xcode错误,我会感到惊讶,但是让我知道当您从调试器检查颜色时会发生什么。

EDIT: 编辑:

So looking into this more there are different color spaces that you can use for RGB. 因此,对此进行更多研究,可以将不同的色彩空间用于RGB。 By default interface builder is using a different one that iOS uses. 默认情况下,界面构建器使用的是iOS使用的另一种。 To switch them click the little gear when you are using the color picker and set it to Generic RGB. 要切换它们,请在使用颜色选择器时单击小齿轮,然后将其设置为“通用RGB”。 在此处输入图片说明

You can do it in such way 你可以这样做

例

And code for another view: 并为另一个视图编写代码:

self.testView.backgroundColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0];

UPDATE: 更新:

In Xcode, click the colorspace popup in the color picker and choose "Generic RGB", then enter the red, green and blue values from Photoshop, NOT THE HEX VALUE 在Xcode中,单击颜色选择器中的颜色空间弹出窗口,然后选择“通用RGB”,然后输入Photoshop中的红色,绿色和蓝色值,而不是十六进制值

And we can find so many duplicates: 我们可以找到很多重复项:

Wrong color in Interface Builder's color picker Interface Builder的颜色选择器中的颜色错误

Weird colors in XCode Interface Builder? XCode Interface Builder中奇怪的颜色?

Wrong color in Interface Builder Interface Builder中的颜色错误

Xcode 6 beta color picker issue Xcode 6 Beta颜色选择器问题

... ...

This whole issue is fixed by Apple in El Capitan. 整个问题已由Apple在El Capitan中解决。 So if you are using xcode 7 on El Capitan this is no longer an issue. 因此,如果您在El Capitan上使用xcode 7,则不再是问题。 Entering #hex color value doesn't change color space anymore. 输入#hex颜色值不再更改颜色空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法更改为IB中的不同大小类定义的约束IBOutlet - Can't change Constraint IBOutlet that is defined for different size classes in IB 在UITableViewCell上使用IB的“用户定义的运行时属性” - Using IB's “User Defined Runtime Attributes” on a UITableViewCell 当在ViewController上定义了`IBAction` /`IBOutlet`时,是否可以在IB中对其进行访问? - Will `IBAction`/`IBOutlet` be accessible in the IB when they arent defined on the ViewController? 在ib中布置应用时,格式与模拟器中的格式不同 - Formatting is different when laying out app in ib than in simulator 标头中定义的静态布尔仅适用于一个文件,为什么? - static bool defined in header works for one file only, why? 通过代码在IB链接的UITextView上设置自定义字体/ color - Setting custom font /color on IB-linked UITextView through code 为什么我的标签栏控制器从活动视图控制器之外的视图控制器执行代码? - Why does my tabbar controller execute code from a different view controller than the active one? 为什么未定义cordova? - Why cordova is not defined? IB颜色问题 - IB Color Issues ib/main.dart:49:24:错误:未为 class 'Set 定义运算符'[]' <map<string, object> &gt;' </map<string,> - ib/main.dart:49:24: Error: The operator '[]' isn't defined for the class 'Set<Map<String, Object>>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM