Support Dark Mode in Legacy iOS App on iOS 13

Today I updated You Doodle for iOS 13 and added dark and light mode support. For a legacy application with all objective-c code and no storyboards, swift ui or other “goodies”, I needed a way to use my existing theming and appearance proxy code.

I chose to support dark/light mode in the application did become active event. I already have my own theming code that changes all the icons and colors and notifies anyone who is interested that the theme has changed.

Here is the code, you’ll need to of course swap it out with your own root view controller, and get/set app theme methods and enums.

- (void) applicationDidBecomeActive:(UIApplication *)application
{
    if (@available(iOS 13, *))
    {
        if ([[RootViewController sharedInstance] respondsToSelector:@selector(setOverrideUserInterfaceStyle:)] &&
            [[RootViewController sharedInstance] respondsToSelector:@selector(traitCollection)])
        {
            RootViewController.sharedInstance.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
            if (RootViewController.sharedInstance.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark)
            {
                SetAppTheme(AppThemeDark);
            }
            else if (RootViewController.sharedInstance.traitCollection.userInterfaceStyle == UIUserInterfaceStyleLight)
            {
                switch (GetAppTheme())
                {
                    case AppThemeDark:
                        SetAppTheme(AppThemeLight);
                        
                    default:
                        // I provide other themes like colorful, modern, pink/sparkles and I don't want to override with the light theme in those cases
                        break;
                }
            }
        }
    }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments