I need to implement the dark mode in my iOS app which runs on iOS 8+. Also I have custom colors defined in color assets with RGB codes for both light and dark appearance.
The problem is named colors are not supported proir to iOS 11, but I can use only named colors to support both appearances - dark and light.
Is there any way to implement a backward compatibility?
You can't do it while using color assets. However you can create your own
ThemeMangerto handle the app's theme.First create a
ThemeManager. It's main purpose is to hold thestyleof the app.Here
ThemeStyleshould be an enum containing your themes(light, dark etc.).Create a
ThemeColorclass to handle your colorsYou can use the
getColorfunction to get required color. Notice thatThemeColorreturns theassetcolor value for iOS 13. This is for changing the app's theme when user changes his/her preference from settings.Example:
stylevariable inThemeMangerbased on user's preference for iOS 13 devices. You can do this bytraitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)implementing this method in yourUIViewControllerclass.backgroundColorexample you need to define all your colors which are effected by the theme change.stylevalue of yourThemeMangerto your preferredstyleso that your app opens in it's default theme when not running on iOS 13.Edit: If your users won't be able to change the app's theme on their own from within the app itself then you don't need the
lightanddarkUIColor values inThemeColor. Just define adefaultColorvalue and return it at the end ofgetColorfunction. This will allow your app to appear in it's default theme for devices running iOS 12 or lower.