iOS 12 中新 iOS 13 种系统颜色的后备行为

Fallback behavior for new iOS 13 system colors in iOS 12

我目前正在采用深色模式,我认为在 Interface Builder 中使用 systemBackgroundlabel 等新系统颜色也 有效当 运行 应用程序处于 iOS 12 时。我有一半希望得到编译器错误,但应用程序看起来像 iOS 13 轻模式。所以很明显,运行时以某种方式将这些颜色转换为 iOS 12.

有谁知道引擎盖下发生了什么,是否有方便的方法在代码中实现相同的功能?

如果您查看故事板的 XML,您会看到如下内容:

<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>

Xcode 11 正在添加对两种颜色的支持。无论哪种机制在运行时将 .storyboard 文件转换为动作,它都需要知道哪种颜色用于 iOS 13 以及哪种颜色用于 iOS 12 及更早版本。

在代码中您需要如下内容:

extension UIColor {
    class var mySystemBackground: UIColor {
        if #available(iOS 13, *) {
            return .systemBackground
        } else {
            return .white
        }
    }
}