Digital Ruby Software – Home

Greetings. I’m Jeff Johnson and I founded Digital Ruby in 2009 in order to learn how to run a software company, teach myself mobile programming and expand my skills and knowledge. I craft iOS apps, websites and server software.

Support Dark Mode in Legacy iOS App on iOS 13

Sep 19, 2019

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



My latest blog posts: