在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 } } }
如果需要对体验要求高,也可以加上动画
UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: { if self.overrideUserInterfaceStyle == .dark { self.overrideUserInterfaceStyle = .light } else{ self.overrideUserInterfaceStyle = .dark } }, completion: nil)