A. Samuel
A. Samuel

Reputation: 39

Dark Mode Switch on App

I have in my project (Swift) a TableViewController and also a ViewController. I have a switch that changes the color of my app (to dark). The problem is that it only changes it in the scene that I am in. If I go to another scene, it's white.

My code:

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

Upvotes: 2

Views: 2556

Answers (1)

Shades
Shades

Reputation: 5616

Use User defaults to save that switch state in your app:

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

And then move all the view-changing code to the viewDidLoad() of each view controller in which you want the color to change:

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

Upvotes: 5

Related Questions