实时更新UserDefaults更改

时间:2018-09-10 21:15:31

标签: ios swift nsuserdefaults

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

1 个答案:

答案 0 :(得分:0)

在功能中隔离主题代码:

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