Named colors via storyboard

Roe*_*e84 4 macos colors storyboard ios

For my MacOS app, I created a colors Asset catalog. The Deployment target for my project is 10.11.

When I set colors at the storyboard, I can choose from the colors asset. But when I try to set one of the named colors programmatically, I need to wrap it in

if #available(OSX 10.13, *) {
   view.backgroundColor = NSColor(named: "someColor")
} else {
   // Fallback on earlier versions
}
Run Code Online (Sandbox Code Playgroud)

ORIGINAL QUESTION:

So the question is why I'm not getting any warnings for choosing those colors via the storyboard?

I guess I need to replace them with a custom color, or my app will crash on OS < 10.13, but I would expect the storyboard not to let me choose those colors because of the deployment target..

EDITED:

So the question is what happens when I'm using those colors via Storyboard? Would it crash the app? or maybe the storyboard can handle named colors even for OS versions < 10.13 ? Because I tried to use the named colors via the Storyboard, and I installed the app on a Mac with OS 10.12, and the app worked just fine. So I'm wondering what's the reason for that..

P.S This question is also relevant to iOS, where the min version for named colors is iOS 11

Jak*_*kub 5

不会使您的应用程序崩溃。使用资产目录中的颜色,您有两个选择:

  1. 仅将其与故事板一起使用。
  2. 使用UIColor(named: <#String #>)像你想在你的问题做。

唯一的问题是UIColor(named:)在 iOS 11.0+ 中可用(请参阅文档

因此,如果您想在每次需要考虑回退到最旧版本时都使用它,这可能不是您想要的。

但是在媒体库中的 Xcode 中内置了另一个解决方案:

在此处输入图片说明

您可以从资产目录中拖放颜色,Xcode 将为您创建此颜色的参考,该颜色也适用于 iOS 10.0。

隐藏在这种颜色中的实际代码行是:

colorLiteral(red: 0.9411764706, green: 0.07843137255, blue: 0.07843137255, alpha: 1)

因此,您可以编写一个脚本来为您生成这种颜色。

前段时间我需要类似的解决方案,所以我确实写了一个脚本来扫描我整个项目的资产目录,并在其中找到一个颜色集并创建UIColor包含来自资产的所有颜色的扩展:

你可以在这里找到它:https : //github.com/JakubMazur/SwiftColorsGenerator,它可以通过马拉松使用(https://github.com/JohnSundell/Marathon)