实时更新 UserDefaults 更改

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)
}