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.