2

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
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
A. Samuel
  • 39
  • 6
  • Include some code to show what you are doing. Generally you will need to copy the switch state between views, either by using a segue where you can capture the destination view controller and set a property before it is displayed, or by saving the switch state to `UserDefaults`, or by implementing a singleton state class. – Matt H Sep 22 '17 at 16:20
  • I pasted my code here. Instructions with screenies are better for me since I'm new at this, but I will take that on note. – A. Samuel Sep 22 '17 at 16:29

1 Answers1

5

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
    }
}
Shades
  • 5,568
  • 7
  • 30
  • 48
  • Hey Shades, thanks for the help, but I actually didn't understand you, can you like explain it to me step by step? or sending screenshots, like I don't know how to retrieve it on ViewController and stuff like that, thanks. – A. Samuel Sep 22 '17 at 16:50
  • @A.Samuel There's nothing to screenshot. You're saving the preference chosen by the user (dark or light) and then you are retrieving that saved setting inside each view controller. Read about Userdefaults: https://stackoverflow.com/questions/31203241/how-to-use-nsuserdefaults-in-swift – Shades Sep 22 '17 at 16:52
  • oh wait something happened, I closed the app and now the dark mode is permanent, I mean, it's black now, and even if I tap the switch, it doesn't change to white. – A. Samuel Sep 22 '17 at 17:07
  • @A.Samuel If you want the color to change right away, copy that code back into the IBAction (or better yet put it all in a function fore reuse), but also make sure to leave it in the viewdidLoad. – Shades Sep 22 '17 at 17:11
  • ok, now when I tap on the switch, it only changes the color back to white only in that scene, but it leaves the other one in dark. – A. Samuel Sep 22 '17 at 17:17
  • Again, nevermind, I fixed it :D, the only thing I need know is to know how to leave the switch pressed after closing the app, but probably searching will help me, thank you so much Shades – A. Samuel Sep 22 '17 at 17:21