ios - iOS 浅色模式和深色模式
问题描述
我想在我的应用程序中拥有一项功能。我决定创建 UIAlertController 和 2 AlertAction:Light Mode 和 Dark Mode。如何通过单击其中一个 AlertAction 来更改整个应用程序的模式。请帮忙 ....
解决方案
@available(iOS 13.0, *)
func changeMode() {
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "DARK MODE", style: .default, handler: { action in
UIWindow.animate(withDuration: 0.5, animations: {
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .dark
//also try : UIApplication.shared.windows.last?.overrideUserInterfaceStyle = .dark
})
}))
alertController.addAction(UIAlertAction(title: "LIGHT MODE", style: .default, handler: { action in
UIWindow.animate(withDuration: 0.5, animations: {
UIApplication.shared.keyWindow?.overrideUserInterfaceStyle = .light
//UIApplication.shared.windows.last?.overrideUserInterfaceStyle = .light
})
}))
self.present(alertController, animated : true)
}
推荐阅读
- python - Prime numbers using sieve of erathosthenes
- regex - 出于管理目的将 /etc/shadow 中的密码替换为屏蔽文本
- compiler-errors - ld:找不到-lSystem collect2的库:错误:ld返回1退出状态gfortran
- python-3.x - 刮硒保护地
- c++ - 使用openmp实现并行广度优先搜索
- python - Python 从错误的目录导入包(在 virtualenv 之外)
- ruby-on-rails - 在 Ruby on Rails HTML 文件中检查 verify_recaptcha 的结果
- ionic-framework - 如何从离子中的查询字符串中获取参数?
- c - 在链表中,程序在进入结构的成员时关闭
- c++ - 使用 C++ 的正则表达式