暗模式开启应用

时间:2017-09-22 16:18:35

标签: ios swift

我的项目(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
        }
    }
}

1 个答案:

答案 0 :(得分:3)

使用用户默认值在您的应用中保存该切换状态:

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

然后将所有改变视图的代码移动到每个视图控制器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
    }
}