Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fallback behavior for new iOS 13 system colors in iOS 12

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?

like image 613
Frank Schlegel Avatar asked Aug 05 '19 18:08

Frank Schlegel


1 Answers

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
        }
    }
}
like image 195
rmaddy Avatar answered Oct 15 '22 19:10

rmaddy