Is it possible to manually set / toggle the UserInterfaceStyle / Dark Mode per App?

992 views Asked by At

I have updated my iOS 11+ to support Dark Mode when being used on iOS 13. This works fine when the UserInterfaceStyle / Dark Mode is enabled or disable in the device settings.

However, I would like to give users the option to activate / deactivate Dark Mode for my app only.

Using a switch to override / toggle the UserInterfaceStyle of the current app window is no big deal:

if #available(iOS 13.0, *) {
    for window in UIApplication.shared.windows {
        window.overrideUserInterfaceStyle = (uiStyleSwitch.isOn ? .dark : .light)
    }
}

But this approach has two one downsides:

  • I would like to toggle between Dark Mode, Light Mode and System preferences where the first two forces one mode or the other over the current system settings and the third uses the system settings. But how to go back to "use system settings" after overrideUserInterfaceStyle has been set? Using .unspecified seems to work, but the behaviour is not really documented. Is it save to be used? Can be solved using .uspecified (see comments)

  • This only works for the current available windows. However, different 3rd party controls and libraries create their own windows (e.g. to display toast message, popups, etc.). Since I cannot control the creation of these windows I cannot override their style.

So the question is: Is it possible to dynamically override the UserInterfaceStyle for the complete app?

I know that I can choose the style in the Info.plist but this cannot be changed by the user of course.

1

There are 1 answers

2
shim On

Use .unspecified to revert to the system settings for the interface style.

Choose this option when you want to follow the system's interface style

As for the other part of your question, it would probably depend on what is being presented and what callbacks you can access.