我支持暗模式。在我的一些视图控制器中,我traitCollectionDidChange(_)
用于处理用户界面样式更改。
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13.0, *) {
if let p = previousTraitCollection {
print("TRAIT COLLECTION \(p.userInterfaceStyle.desc) -> \(traitCollection.userInterfaceStyle.desc). \(UIApplication.shared.applicationState.desc); \(p.hasDifferentColorAppearance(comparedTo: traitCollection) ? "TRAIT CHANGED" : "TRAIT SAME")")
}
}
}
当我按下主页按钮并进入后台时,此方法会触发两次:
TRAIT COLLECTION DARK -> LIGHT. BACKGROUND; TRAIT CHANGED
TRAIT COLLECTION LIGHT -> DARK. BACKGROUND; TRAIT CHANGED
当我在 iOS 设置中更改外观并返回前台时,我得到
TRAIT COLLECTION DARK -> LIGHT. INACTIVE; TRAIT CHANGED
奇怪的。每次我进入后台时,我都会来回更改用户界面样式。为什么?