Fallback behavior for new iOS 13 system colors in iOS 12

▼魔方 西西 提交于 2019-12-10 10:29:21

问题


I’m currently adopting to Dark Mode and I figured that using the new system colors like systemBackground and label in Interface Builder also just works when running the app in iOS 12. I half expected to get a compiler error, but instead the app looks like in iOS 13 light mode. So obviously the runtime somehow translates those colors for iOS 12.

Does anyone know what happens under the hood and if there is a convenient way to achieve the same in code?


回答1:


If you look at the XML of the storyboard, you will see something like:

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

Xcode 11 is adding support for both colors. Whatever mechanism converts the .storyboard file into actions at runtime has the information it needs to know which color to use for iOS 13 and which color to use for iOS 12 and earlier.

In code you need something like the following:

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


来源:https://stackoverflow.com/questions/57364611/fallback-behavior-for-new-ios-13-system-colors-in-ios-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!