ios13 - 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?
解决方案
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.
推荐阅读
- python - 如何使用拟合的 MinMaxScalar 创建 DataFrameMapper 管道?
- visual-studio-code - 忽略tasks.json中先前构建任务的错误
- karate - 空手道 Http 请求有条件地添加参数
- javascript - 使用javascript自动将文件附加到输入
- angular - 基于布尔角反应形式有条件地应用 mat-form-field-invalid
- flutter - Flutter:如何为 Slider 小部件创建自定义拇指?
- c++ - VS2019 中的 C++20 chrono 解析问题(最新)
- dax - 在 Power BI 中加入具有一个公共字段的卡车交付事务表
- php - 如何在 PHP 中将“2021 年 6 月 18 日 21:33”转换为“2021-06-18 21:33:00”?
- python - 上传 gcs 存储桶中所有必要文件时的 apache 光束触发