hasDifferentColorAppearance is true when app is backgrounded

夙愿已清 提交于 2019-12-31 00:45:51

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!