简体   繁体   English

XCode 8 不正确的背景颜色 - 故事板更新的色彩空间

[英]XCode 8 incorrect background color - Storyboard Updated Color Space

After upgrading to XCode 8, I noticed some of my views (layed out in Interface Builder) now have incorrect colors.升级到 XCode 8 后,我注意到我的一些视图(在 Interface Builder 中布局)现在有不正确的颜色。

Specifically, I have two custom views on my login page which normally have a white background.具体来说,我的登录页面上有两个自定义视图,它们通常具有白色背景。 After upgrading to XCode 8, their background color is clear!升级到 XCode 8 后,它们的背景颜色清晰了! Looks terrible.看起来很可怕。

I've done some investigation and found this from Apple:我做了一些调查,发现这个来自苹果:

Color values in Interface Builder documents correctly use color space during rendering and compilation. Interface Builder 文档中的颜色值在渲染和编译期间正确使用颜色空间。 Earlier versions of Xcode mishandled color spaces saved in iOS and tvOS documents.早期版本的 Xcode 对保存在 iOS 和 tvOS 文档中的色彩空间处理不当。 Xcode 8 converts existing colors in a way that preserves their perceptual appearance on device, and updates either the color space or component values in the source document as appropriate. Xcode 8 以一种在设备上保留其感知外观的方式转换现有颜色,并根据需要更新源文档中的颜色空间或组件值。 (7645087) (7645087)

And sure enough, when I look at the diff for the commit that broke things:果然,当我查看破坏事物的提交的差异时:

This:这个:

<rect key="frame" x="8" y="8" width="88" height="48"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>

Was replaced by this:被替换成这样:

<color key="backgroundColor" red="0.0" green="0.0" blue="0.0" alpha="0.0" colorSpace="custom" customColorSpace="sRGB"/>

What can I do to fix this?我能做些什么来解决这个问题? Here are some things I have tried:以下是我尝试过的一些事情:

1) Within interface builder, select the view, and open the color pallet for background color. 1) 在界面生成器中,选择视图,然后打开背景颜色的调色板。 Click the gear icon, and select SRGB.单击齿轮图标,然后选择 SRGB。 Tried other profiles as well, nothing seems to work.也尝试了其他配置文件,似乎没有任何效果。

2) Tried reverting the XIB file. 2) 尝试恢复 XIB 文件。 Can't do this since it was part of XCode 8 upgrade.不能这样做,因为它是 XCode 8 升级的一部分。 This change to the color profiles happens when you call "Update Frames" for the view controller, after selecting the iPhone size you want.在选择所需的 iPhone 尺寸后,当您为视图控制器调用“更新帧”时,会发生对颜色配置文件的这种更改。

Anyone have any other suggestions would be so helpful!任何人有任何其他建议都会很有帮助!

Incredible.极好的。

In my custom view (subclass of UIView), I had a "commonInit" method, which held all of the common instantiation code called via "init", "initWithCoder:" or "initWithFrame:"在我的自定义视图(UIView 的子类)中,我有一个“commonInit”方法,它保存了所有通过“init”、“initWithCoder:”或“initWithFrame:”调用的通用实例化代码

Within that "commonInit" method, I was doing something I probably shouldn't have been doing:在那个“commonInit”方法中,我正在做一些我可能不应该做的事情:

self.layer.cornerRadius = 0.5 * self.bounds.size.height;

Somehow, this worked on iOS 9.3 + XCode 7, but NOT IOS10 + XCode 8不知何故,这个工作在iOS 9.3 + 7的XCode,但不是IOS10 + 8的XCode

It makes sense why it shouldn't work.为什么它不应该工作是有道理的。 At time of instantiation, the view hasn't yet been layed out, and so it's frame is likely {0,0,0,0}.在实例化时,视图尚未布局,因此它的框架可能是 {0,0,0,0}。

The solution was to move that code into "layoutSubviews".解决方案是将该代码移动到“layoutSubviews”中。

The question remains as to why the behavior resulted in a clear background.问题仍然是为什么这种行为会导致一个清晰的背景。 Probably, because a cornerRadius of 0 is not supported?可能是因为不支持为 0 的角半径?

I've had the same problem about custom views (the background color can't be set).我在自定义视图方面遇到了同样的问题(无法设置背景颜色)。 With some searching and trial and error, my problem has been resolved.通过一些搜索和反复试验,我的问题已经解决。 The tips to resolve the problem are:解决问题的提示是:

  1. when you open xib, there will show a notice to "choose an initial device view", and there will be two options;打开xib,会出现“选择初始设备视图”的提示,有两个选项; you can choose the "choose Device".您可以选择“选择设备”。

  2. find out "interface builder Document" in the right sidebars and reset the "open in" to be Xcode 7.x.在右侧边栏中找到“界面构建器文档”并将“打开方式”重置为 Xcode 7.x。 After setting that there will be a notice to save your settings;设置好后会有提示保存设置; you should choose "save and close".您应该选择“保存并关闭”。

Following these steps may resolve your issue.按照这些步骤操作可能会解决您的问题。

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

相关问题 Xcode @IBDesignable按钮背景颜色未在情节提要中呈现 - Xcode @IBDesignable Button Background Color Not Rendering In Storyboard 如何在Xcode中更改故事板的背景颜色? - How to change background color of storyboard in Xcode? 在Xcode 8.2.1中更改没有Storyboard和ViewController的背景颜色 - Change background color without storyboard and viewController in Xcode 8.2.1 Xcode Storyboard Preview 不显示由代码添加的背景颜色 - Xcode Storyboard Preview doesn't show Background Color added by code Xcode 12.4 storyboard 无法更改视图的背景颜色 - Xcode 12.4 storyboard can not change background color of a View XCODE:UIView 背景没有出现在屏幕上,即使我在情节提要和编程中设置了颜色 - XCODE: UIView background not appearing on screen, even though I set color in storyboard and programmatically 为什么我的 Storyboard 在 Xcode 中的背景颜色没有改变? 它总是相同的灰色? - Why doesn't the background colour of my Storyboard in Xcode change? It's always the same grey color? 通过更改颜色空间,在背景上最流行的颜色 - Most prevalent color on a background by changing color space 如何在 Xcode storyboard 拾色器中添加自定义颜色? - How to add custom color in Xcode storyboard color picker? Storyboard 未在 XCode 12.0.1 中显示自定义颜色十六进制代码 - Storyboard is not showing custom color hex code in XCode 12.0.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM