How to switch programmatically to dark mode swift

Solution 1:

You can override the style for single views or view controller using the overrideUserInterfaceStyle property. But since the window is also a view, you can set that on your main window to force it into light or dark mode:

window.overrideUserInterfaceStyle = .dark

Solution 2:

You can use one of the observation ways, for example, Defaults lib, and then add

window.overrideUserInterfaceStyle = .dark

to

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {}

method