iOS 11中Xcode 11 beta命名颜色存在问题

4

我正试图在我的iOS应用程序中实现暗色模式。我需要使用命名颜色来实现这个目标,但是遇到了一些问题。在iOS 13模拟器上在浅色和暗色模式之间切换时一切正常,并且iOS 12模拟器中的颜色也是正确的。问题出现在我尝试在iOS 11模拟器上运行应用程序时。任何我在Storyboard中使用的命名颜色都会默认为该颜色的暗模式,在代码中访问一个命名颜色时,我得到nil值。只是想知道是否有其他人遇到过这个问题。


我需要使用具名颜色来完成这个任务,你可以在代码中配置颜色,这样所有系统都会正确地工作。 - matt
这不是一个非常可行的解决方案,因为我将不得不在代码中设置数百个视图的颜色,而我可以轻松地在Storyboard编辑器中设置它们的颜色。 - rykeeboy
是的,这很麻烦。但是听起来你不会向后兼容iOS 11。提交一个错误报告。 - matt
5个回答

5

这是Xcode 11与IOS 11中的一个漏洞,存在两个与IOS 11中具名颜色相关的问题。

  1. UIColor初始化方法init?(named name: String)在IOS 11中返回nil
  2. Storyboard或xib文件中使用的具名颜色资源有时会默认为深色版。

以下是临时解决方案,直到在即将发布的Xcode更新版本中修复此问题。

  1. If UIColor init method init?(named name: String) returns nil you need to provide fallback color for light mode.

    let color = UIColor(named: "myColor") ?? UIColor.black // default color for IOS 11
    
  2. Named color issue for Story board and xib files, if you observe the changes in your xib file or storyboard after setting any name color you will notice there is a namedColor xml tag under resources tab. And for each namedColor there is fallback color there, that fallback color is being in IOS 11 case because named color isn't working. You can even see the warings in your console.

     <resources>
         <namedColor name="NavigationBar">
             <color red="0.2669999897480011" green="0.70999997854232788" blue="0.046999998390674591" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
         </namedColor>
     </resources>
    

    Bug in Xcode 11 is that default value is set to dark mode version if your MacOS appearance is set to Dark and it picks light color version for default values if your MacOS appearance is set to Light. Workarounds to fix this for IOS 11.

    1. You can set your MacOS appearance to Light. After that you need to open each and every storyboards or xib files in yours project once. Once you open it, editor will automatically pick the Light version of your colors and you can see in your source control that file is modified and default value will be Light version of your color asset. Note that every time your change any value in your color assets, you have to do this again for all the xib files using that named color.
    2. Second is a bit complicated that your write a script that parse all the xib and storyboard files for your project and update the default RGBA values.

很遗憾,这对于Storyboard用户不起作用。 - JBarros35
@lambdapool对于我的storyboards确实有效。但这个问题已经在Xcode 11 GM Seed 2中得到修复,而且公共的Xcode 11更新也已经发布。只需更新您的Xcode即可。 - Bilal

2

看起来苹果公司已经知道这个问题了。

引用自https://developer.apple.com/ios/submit/

请注意,使用命名颜色构建的Xcode 11应用程序在iOS 11或更早版本上运行时可能会出现查找失败(未返回任何值)的情况。这将在未来的Xcode更新中得到修复。为了避免此问题,请将最低部署目标提高到iOS 12或更高版本以立即提交到App Store,或在下一个Xcode GM候选种子可用时重新构建。

更新20190917

使用包括iOS 13、iPadOS、watchOS 6、tvOS 12和macOS Catalina的SDK构建您的应用程序,其中包括Xcode 11 GM种子2。从2020年4月开始,所有提交到App Store的iOS应用程序都需要使用iOS 13 SDK或更高版本构建。它们还必须支持iPhone XS Max或12.9英寸iPad Pro(第3代)或更高版本的全屏设计。


Xcode11的GM2版本已经发布,也许可以修复这个问题。详情请见https://developer.apple.com/news/releases/?id=09162019a。 - Jonny
我可以确认它并没有修复任何东西,我使用的是最新版本11.0 (11A420a),但在IOS上它默认所有内容为白色,无法解决我的问题。 - JBarros35

1

更新Xcode至版本11.0 (11A420a)

对于遇到此问题并使用Storyboards的人,我建议在您的颜色方案中使用“任何外观”默认设置为浅色模式。例如,对于深色模式,我有灰色背景,而浅色则是白色,字体相反,白色和深灰色。将所有内容默认设置为浅色模式,IOS会将其解释为浅色,并且您不会遇到问题。

enter image description here

如您在我的屏幕截图中所见,我的应用程序在IOS 11上可以正常工作。 **输入图像说明** 如果我不这样做,可能所有的字体都会是白色,用户将看不到任何东西。

1

1
我强烈建议将所有颜色分别添加到资产中,并且可以将任何添加的颜色映射到新的系统颜色,这样它就会自动处理iOS 13的浅色模式和深色模式颜色,并且它将采用iOS 12、11的默认颜色...这是自定义颜色

Here how to select one of them in your storyboard

此句话意为:“同时,你可以在代码中使用它,使用方式为”。文本中包含HTML标记,保留不变。
UIColor(named: "systemBlue") ?? UIColor.blue

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接