当应用程序后台运行时,hasDifferentColorAppearance为true [英] hasDifferentColorAppearance is true when app is backgrounded

查看:342
本文介绍了当应用程序后台运行时,hasDifferentColorAppearance为true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple建议我们使用traitCollectionDidChange并使用hasDifferentColorAppearance比较特征集,以在切换黑暗模式时捕获,并在需要时对其进行操作.像这样:

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
        }
    }
}

在黑暗和明亮模式之间切换时,我用它来更新UI,清除一些缓存等.

I use this to update the UI, clear some caches etc when switching between dark and light mode.

由于某些原因,无论我是否在设备上启用了暗模式,每次启动我的应用程序时,都会触发traitCollectionDidChangehasDifferentColorAppearance的评估结果为true.在这种情况下,似乎previousTraitCollection和当前traitCollection永远不会具有匹配的interfaceStyles.如果userInterfaceStyle实际没有更改,我宁愿避免执行当userInterfaceStyle更改时所做的更新.

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都会为浅色外观(以及纵向和横向)创建UI快照.在应用程序切换器用户界面中预览应用程序的背景.因此,这是完全正常的行为.

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.

这篇关于当应用程序后台运行时,hasDifferentColorAppearance为true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆