I've set up a dynamic UIColor
s for OS13 in my Swift app to handle dark mode.
For example:
static var background: UIColor {
if #available(iOS 13, *) {
return UIColor { (traitCollection: UITraitCollection) -> UIColor in
if traitCollection.userInterfaceStyle == .dark {
return UIColor(hex: "2b2d42")
} else {
return UIColor(hex: "FFFFFF")
}
}
} else {
return UIColor(hex: "FFFFFF")
}
}
And wherever in code CGColor
is used instead of UIColor
I was planning to call traitCollectionDidChange
of that particular UIView
like this for example:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
layer.fillColor = color.cgColor
}
It works but what I noticed is, that after changing the mode in settings if I use bottom swipe gesture to switch back to my app, then I can see all the UIColor
s already updated on the incoming screen, but the CGColor
s get updated only when app actually comes to foreground and traitCollectionDidChange
is called.
Is there a better approach to this, so that the CGColor
s also get updated immediately?