Apple recommends that we use traitCollectionDidChange
and compare trait collections using hasDifferentColorAppearance to catch when dark mode is toggled, and act on it if we need to. Like this:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13.0, *) {
let hasUserInterfaceStyleChanged = previousTraitCollection?.hasDifferentColorAppearance(comparedTo: traitCollection) ?? false
if (hasUserInterfaceStyleChanged) {
//Update UI
}
}
}
I use this to update the UI, clear some caches etc when switching between dark and light mode.
For some reason traitCollectionDidChange
fires and hasDifferentColorAppearance
evaluates to true every time my app is backgrounded, no matter if I have dark mode enabled on the device or not. It seems the previousTraitCollection and the current traitCollection never have matching interfaceStyles in this case. I would rather avoid doing the updates I do when the userInterfaceStyle changes if the userInterfaceStyle hasn't actually changed.
Is this a bug, or am I just missing something?
hasDifferentColorAppearance
can return true even when the interfaceStyles are the same. It's other traits that may be different. – Doubletongued