我在我的项目 (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
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用用户默认值在您的应用程序中保存该开关状态:
@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
}
}
Run Code Online (Sandbox Code Playgroud)
然后将所有视图更改代码移动到您希望更改颜色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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2257 次 |
| 最近记录: |