Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hasDifferentColorAppearance is true when app is backgrounded

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?

like image 905
rodskagg Avatar asked Sep 22 '19 18:09

rodskagg


1 Answers

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.

like image 111
Frank Schlegel Avatar answered Oct 23 '22 22:10

Frank Schlegel