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?
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
}
}
}