0

I have a login screen which look like this:

Basically, i want to setup so that after the following actions: when in this login screen -> enter background -> toggle darkmode or lightmode in settings -> enter foreground ,the dropShadowView and its element are changed accordingly. Here down below is my code:

  • in viewDidLoad():
override func viewDidLoad() {
        super.viewDidLoad()
        setupForDarkmode()
        NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
    }
  • And function:
@objc func willEnterForeground() {
        setupForDarkmode()
    }

    private func setupForDarkmode() {
        if #available(iOS 13.0, *) {
            overrideUserInterfaceStyle = .unspecified
            if traitCollection.userInterfaceStyle == .dark {
                dropShadowView.backgroundColor = .black
                userNameTextField.backgroundColor = .black
                userNameTextField.textColor = .white
                passwordTextField.backgroundColor = .black
                passwordTextField.textColor = .white
            } else {
                dropShadowView.backgroundColor = .white
                userNameTextField.backgroundColor = .gray
                userNameTextField.textColor = .blue
                passwordTextField.backgroundColor = .gray
                passwordTextField.textColor = .blue
            }
        }
    }
  • screen's background was already set as system's background color.

However these above code wasn't worked as i expected. The first time it enter foreground right after the change in setting, the dropShadowView and its element keep its appearance until the second time.

After debug a while, i found that right after the change in settings (lightmode -> darkmode for exp), at line if traitCollection.userInterfaceStyle == .dark {, it wasn't recognized current userInterfaceStyle at first (unlike system's background color).

My application's target is lower than iOS 11 so i also have some prolem using colorset.

The question is is there and posible way to update the UI for darkmode programmatically when enter foreground?

Thanks in advance.

Hassan ElDesouky
  • 334
  • 4
  • 17
Hai Hoang
  • 15
  • 7
  • Just for debugging change your `setupForDarkMode()` to `DispatchQueue.main.asyncAfter(deadline: .now()+2.0) { setupForDarkmode() }` inside `willEnterForeground()` and see if it works – Keshu R. Feb 21 '20 at 07:37
  • 1
    If you use dynamic colours then the UI will update automatically. See https://stackoverflow.com/questions/56487679/how-do-i-easily-support-light-and-dark-mode-with-a-custom-color-used-in-my-app and the WWDC 2019 session on adopting dark mode – Paulw11 Feb 21 '20 at 08:17
  • @Paulw11 Thank for cool advise! I will try this out. – Hai Hoang Feb 21 '20 at 08:24

1 Answers1

0

The problem is solved. I used custom color and UI was automatically update: How do I easily support light and dark mode with a custom color used in my app?

It should look like this:

extension UIColor {
    static var dropShadowViewBackground: UIColor {
        if #available(iOS 13.0, *) {
            return UIColor { (traits) -> UIColor in
                // Return one of two colors depending on light or dark mode
                return traits.userInterfaceStyle == .dark ? .black : .white
            }
        } else {
            // Same old color used for iOS 12 and earlier
            return .white
        }
    }
}

This work perfectly!

Hai Hoang
  • 15
  • 7