代码适配
UITraitCollection.current.userInterfaceStyle
if UITraitCollection.current.userInterfaceStyle == .dark {
//暗黑模式
} else {
//其他模式
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13.0, *) {
//是否改变
if self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
//不同
if self.currentStyle == false {
self.view.backgroundColor = .black
self.lbl?.text = "Dark"
self.lbl?.textColor = .white
} else {
self.view.backgroundColor = .white
self.lbl?.text = "Light"
self.lbl?.textColor = .black
}
self.currentStyle = !self.currentStyle
}
} else {
// Fallback on earlier versions
}
}