Background to Foreground Interface style change

When app is moving from background to foreground (detecting via a notification) - it reads the wrong self.traitCollection.userInterfaceStyle i.e. when I change to dark/light when app is in background - what am I doing wrong or not doing at all?
private var observer: NSObjectProtocol?

override func viewDidLoad() {
   super.viewDidLoad()
   self.observer = NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: .main) { [unowned self] _ in
      self.configureNavbar()
   }
}

override func viewWillAppear(_ animated: Bool) {
   super.viewWillAppear(true)
   self.configureNavbar()
}

func configureNavbar() {
   if #available(iOS 12.0, *) {
      if self.traitCollection.userInterfaceStyle == .dark {
         self.navigationController?.navigationBar.barTintColor = .black
      } else {
         self.navigationController?.navigationBar.barTintColor = .white
      }
   }
}

Oh wow, cross posted too early. Use traitCollectionDidChange (answered on iOS Developers slack)

Hi @lganti,
Not sure if the code you have works properly, since Dark Mode was introduced in iOS13 and your conditional is for iOS 12. Secondly you don’t need to really write so much code for colors in dark mode. The colors will change without having to check if it is in foreground or background.

cheers,

Yep, refactored it to to use Color Assets

Hi @lganti,
:+1:t3:

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.