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?
iOS is creating snapshots of your UI for light and dark appearance (and also for portrait and landscape orientation) every time the app is backgrounded for previewing your app in the app switcher UI. So this is perfectly normal behavior.
来源:https://stackoverflow.com/questions/58052390/hasdifferentcolorappearance-is-true-when-app-is-backgrounded