[英]Dark Mode Switch on App
我在我的项目(Swift)中有一个TableViewController和一个ViewController。 我有一个开关可以改变我的应用程序的颜色(变暗)。 问题是它只在我所在的场景中改变它。如果我去另一个场景,它是白色的。
我的代码:
import UIKit
class BaseTableViewController: UITableViewController {
@IBOutlet var InicioTable: UITableView!
@IBOutlet weak var cell2: UITableViewCell!
@IBOutlet var viewTable: UITableView!
@IBOutlet weak var celldarkmode: UITableViewCell!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var switchController: UISwitch!
@IBAction func changeSwitch(_ sender: UISwitch) {
if switchController.isOn == true
{
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//user global variable
self.navigationController?.navigationBar.barStyle = UIBarStyle.black //user global variable
self.navigationController?.navigationBar.tintColor = UIColor.black //user global variable
UIApplication.shared.statusBarStyle = .lightContent
label.textColor = UIColor.white
self.cell2.backgroundColor = UIColor.black
self.tabBarController?.tabBar.barTintColor = UIColor.black
view.backgroundColor = UIColor.init(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
}
else
{
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]//user global variable
self.navigationController?.navigationBar.barStyle = UIBarStyle.default //user global variable
self.navigationController?.navigationBar.tintColor = UIColor.white //user global variable
UIApplication.shared.statusBarStyle = .default
label.textColor = UIColor.black
self.cell2.backgroundColor = UIColor.white
self.tabBarController?.tabBar.barTintColor = UIColor.white
view.backgroundColor = UIColor.groupTableViewBackground
}
}
}
使用用户默认值在您的应用中保存该开关状态:
@IBAction func changeSwitch(_ sender: UISwitch) {
let isDarkMode = userDefaults.bool(forKey: "isDarkMode")
if isDarkMode == true {
UserDefaults.standard.set(false, forKey: "isDarkMode") // Set the state
}
else {
UserDefaults.standard.set(true, forKey: "isDarkMode") // Set the state
}
}
然后将所有视图更改代码移动到要在其中更改颜色的每个视图控制器的viewDidLoad()
:
override func viewDidLoad() {
super.viewDidLoad()
let isDarkMode = UserDefaults.standard.bool(forKey: "isDarkMode") // Retrieve the state
if isDarkMode == true {
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//user global variable
self.navigationController?.navigationBar.barStyle = UIBarStyle.black //user global variable
self.navigationController?.navigationBar.tintColor = UIColor.black //user global variable
UIApplication.shared.statusBarStyle = .lightContent
label.textColor = UIColor.white
self.cell2.backgroundColor = UIColor.black
self.tabBarController?.tabBar.barTintColor = UIColor.black
view.backgroundColor = UIColor.init(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
}
else {
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]//user global variable
self.navigationController?.navigationBar.barStyle = UIBarStyle.default //user global variable
self.navigationController?.navigationBar.tintColor = UIColor.white //user global variable
UIApplication.shared.statusBarStyle = .default
label.textColor = UIColor.black
self.cell2.backgroundColor = UIColor.white
self.tabBarController?.tabBar.barTintColor = UIColor.white
view.backgroundColor = UIColor.groupTableViewBackground
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.