IOS 黑暗模式简单理解
在IOS13的时候,苹果引入了Dark模式,也就是夜间模式。
dark模式支持在各种层级的UI控件下设置,比如UIWindow、UIViewController、UIView等。并且支持动态切换
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func changeThemeHanlder(_ sender: Any) {
if overrideUserInterfaceStyle == .dark {
overrideUserInterfaceStyle = .light
}
else{
overrideUserInterfaceStyle = .dark
}
}
}
![](https://img2020.cnblogs.com/blog/653862/202201/653862-20220110221732377-655853233.gif)
如果需要对体验要求高,也可以加上动画
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
if self.overrideUserInterfaceStyle == .dark {
self.overrideUserInterfaceStyle = .light
}
else{
self.overrideUserInterfaceStyle = .dark
}
}, completion: nil)