gpt4 book ai didi

ios - 实时更新 UserDefaults 更改

转载 作者:行者123 更新时间:2023-11-30 11:10:10 24 4
gpt4 key购买 nike

当用户点击按钮时,它会更改 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)
}

关于ios - 实时更新 UserDefaults 更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com