10

I'm using Swift 5.1 and Xcode 11.1 and I've currently finished implementing Dark Mode design.

App has setting where user can set display theme(Light, Dark, System Default) and currently it's working fine if app restarts after user selects theme(I save this bool data in UserDefaults and set UIAppearance at app startup in AppDelegate file)

Here's my code

if #available(iOS 13.0, *) {
   switch AppState.appThemeStyle {
   case "dark":
       window?.overrideUserInterfaceStyle = .dark
       break
   case "light":
       window?.overrideUserInterfaceStyle = .light
       break
   default:
       window?.overrideUserInterfaceStyle = .unspecified
   }
}

But I can see that many apps change display themes immediately after user sets theme style.

I think It's not good idea to restart app for only changing theme and theme should change immediately after user sets theme style.

I tried to do that by setting base viewcontroller and set user interface style on ViewWillAppear but Navigation bar & Tab bar appearance doesn't change.

Could anyone please tell me how to handle this? Thanks.

Ioan Moldovan
  • 2,272
  • 2
  • 29
  • 54

2 Answers2

8

This code is instant and there is no need to restart the app!

You just need to call the code on user interaction with notification observer pattern or directly call the function.

But the point is to access the visible window and overrideUserInterfaceStyle there. So for example you can set it from any visible view like:

view.window?.overrideUserInterfaceStyle = .dark

Deprecated but working method

// Because you are using AppDelegate for this: (But it is going to be deprecated. read the following Note)
(UIApplication.shared.delegate as! AppDelegate).myFunctionThatChangesTheUserInterfaceStyle()

Also note that since iOS 13, the key window is not a part of appDelegate anymore and you should access it from sceneDelegate

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
  • You are right and I upvoted your answer, but I think you need to use guard let , or if let when accessing Appdelegate. – Ioan Moldovan Oct 28 '19 at 10:19
  • Force unwrap is perfectly fine here because no one can be app delegate instead of `AppDelegate` – Mojtaba Hosseini Oct 28 '19 at 10:20
  • I agree, But one more question, how can I change status bar text color depends on theme change? – Ioan Moldovan Oct 28 '19 at 10:32
  • That is `viewController` based. Please feel free to [ask a new question](https://stackoverflow.com/questions/ask) and comment the link to it here. So I can help you with more description. – Mojtaba Hosseini Oct 28 '19 at 10:35
  • Here it is. Could you please check and help? https://stackoverflow.com/questions/58589634/how-to-change-status-bar-text-color-immediately-in-swift-in-ios-13 – Ioan Moldovan Oct 28 '19 at 10:54
1

We can change user interface style immediately by setting overrideUserInterfaceStyle. I added this code where user can select theme style.

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
   return
}
appDelegate.changeTheme(themeVal)

I hope this will help others

Ioan Moldovan
  • 2,272
  • 2
  • 29
  • 54