[英]Update UserDefaults changes in real time
當用戶點擊按鈕時,它將更改UserDefaults以更改背景顏色,從而啟用暗模式。 但是我希望當用戶點擊我的按鈕時,顏色變化過渡會實時發生,就像Twitter應用程序所做的那樣,而不必每次都重新打開應用程序。 有什么辦法可以做到嗎?
這是按鈕的代碼:
@IBAction func changeState(_ sender: UIButton) {
let isDarkMode = UserDefaults.standard.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
}
}
這是我用來更新UI顏色的代碼:
override func viewDidLoad() {
super.viewDidLoad()
let isDarkMode = UserDefaults.standard.bool(forKey: "isDarkMode") // Retrieve the state
if isDarkMode == false{
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//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
appVersionLabel.textColor = UIColor.black
lightModeLabel.isHidden = true
self.tabBarController?.tabBar.barTintColor = UIColor.black
settingsView.backgroundColor = UIColor.white
darkModeButton.setImage(UIImage(named: "darkModeDisableButton")?.withRenderingMode(.alwaysOriginal), for: .normal)
}else{
UIApplication.shared.statusBarStyle = .lightContent
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//user global variable
self.navigationController?.navigationBar.barStyle = UIBarStyle.default //user global variable
self.navigationController?.navigationBar.tintColor = UIColor.white //user global variable
UIApplication.shared.statusBarStyle = .lightContent
appVersionLabel.textColor = UIColor.white
darkModeLabel.isHidden = true
self.tabBarController?.tabBar.barTintColor = UIColor.black
settingsView.backgroundColor = UIColor(red: 35/255, green: 35/255, blue: 35/255, alpha: 1)
darkModeButton.setImage(UIImage(named: "darkModeEnableButton")?.withRenderingMode(.alwaysOriginal), for: .normal)
}
將主題代碼隔離在一個函數中:
func changeMode(mode: Bool) {
let bar = self.navigationController?.navigationBar
if mode == false {
bar?.isTranslucent = false
bar?.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//user global variable
bar?.barStyle = UIBarStyle.default //user global variable
bar?.tintColor = UIColor.white //user global variable
UIApplication.shared.statusBarStyle = .default
appVersionLabel.textColor = UIColor.black
lightModeLabel.isHidden = true
self.tabBarController?.tabBar.barTintColor = UIColor.black
settingsView.backgroundColor = UIColor.white
darkModeButton.setImage(UIImage(named: "darkModeDisableButton")?.withRenderingMode(.alwaysOriginal), for: .normal)
} else {
UIApplication.shared.statusBarStyle = .lightContent
bar?.isTranslucent = false
bar?.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//user global variable
bar?.barStyle = UIBarStyle.default //user global variable
bar?.tintColor = UIColor.white //user global variable
UIApplication.shared.statusBarStyle = .lightContent
appVersionLabel.textColor = UIColor.white
darkModeLabel.isHidden = true
self.tabBarController?.tabBar.barTintColor = UIColor.black
settingsView.backgroundColor = UIColor(red: 35/255, green: 35/255, blue: 35/255, alpha: 1)
darkModeButton.setImage(UIImage(named: "darkModeEnableButton")?.withRenderingMode(.alwaysOriginal), for: .normal)
}
}
您的viewDidLoad()
如下所示:
override func viewDidLoad() {
super.viewDidLoad()
let isDarkMode = UserDefaults.standard.bool(forKey: "isDarkMode") // Retrieve the state
changeMode(mode: isDarkMode)
}
還有您的IBAction:
@IBAction func changeState(_ sender: UIButton) {
let isDarkMode = UserDefaults.standard.bool(forKey: "isDarkMode")
//Pointed out by rmaddy
UserDefaults.standard.set(!isDarkMode, forKey: "isDarkMode")
changeMode(mode: !isDarkMode)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.