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:
override func viewDidLoad() {
super.viewDidLoad()
setupForDarkmode()
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
}
@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
}
}
}
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.
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!