代码适配
- 获取当前的模式
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
}
}
图片适配
-
在Assets.xcassets中,点击图片-右边第四个(command+option+4快捷键),里面有个Appearance,选择Any,Dark,最后将暗黑模式所需要的图片拖进去即可。
image.png
网友评论