0

I am trying to make it so that the user, when turning dark mode on(using the switch), would change the table view cells background color to black(hence being a dark mode). I was also wondering on how to change the color of the navigationbar when the switch is on as well.

Below is what I have tried(full code):

import Foundation
import UIKit

class SideMenuController8: UITableViewController{

    @IBOutlet var TableViewColor: UITableView!

    @IBOutlet weak var OpenSettings: UIBarButtonItem!
    @IBOutlet weak var mSwitch: UISwitch!
    @IBOutlet weak var dSwitch: UISwitch!
    override func viewDidLoad() {

        self.navigationController?.navigationBar.topItem!.title = "Settings"


        if revealViewController() != nil {
            OpenSettings.target = revealViewController()
            OpenSettings.action = #selector(SWRevealViewController.revealToggle(_:))
            view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }




//        mSwitch.layer.borderWidth = 1
//        mSwitch.layer.borderColor = UIColor.white.cgColor
        let onColor  = UIColor(red: CGFloat(0.0), green: CGFloat(122.0 / 255.0), blue: CGFloat(1.0), alpha: CGFloat(1.0))
        let offColor = UIColor.white

        //Notifications On Switch
        mSwitch.isOn = false
        /*For on state*/
        mSwitch.onTintColor = onColor
        /*For off state*/
        mSwitch.tintColor = offColor
        mSwitch.layer.cornerRadius = 16
        mSwitch.backgroundColor = offColor

        //Dark Mode Switch
        dSwitch.isOn = false
        /*For on state*/
        dSwitch.onTintColor = onColor
        /*For off state*/
        dSwitch.tintColor = offColor
        dSwitch.layer.cornerRadius = 16
        dSwitch.backgroundColor = offColor




            if (dSwitch.isOn == true){

                TableViewColor.reloadData()
                print("Dark Mode Switch is on")

            }


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

Image

Here is another pic showing my main storyboard

enter image description here

Rajat Khare
  • 522
  • 8
  • 26

2 Answers2

2

For the background color of the UITableViewCells, you could have a boolean that indicates if the night mode is on, as you have now.

When there is a change in the switch value (from off to on or viceversa), you should call tableView.reloadData(). This way, the method cellForIndexPath will be called again for each one of the cell. In this method, you should check if the night mode is on (with the boolean) and therefore set the backgroundColor of the cell accordingly.

For the navigationBar, you could use the property called barTintColor. You can use it the following way

UINavigationController?.navigationBar.barTintColor = //your color

Also, remember that you should implement the datasource methods of the tableView. Since your tableviewController is already the datasource and delegate, you just have to override them. There are 3 important ones.

//Number of sections
override func numberOfSections(in tableView: UITableView) -> Int

//Number of rows in a section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

//In this one you setup or return a tableviewcell for the specific
//IndexPath. Here you should create a UITableViewCell and set its background
//color accordingly to the value of the switch
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

This methods are basic for the UITableViewController. This may be out of scope for the question, you could check out more in several sources out there. This is an example that explains a little bit more https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/

Federico Ojeda
  • 758
  • 7
  • 11
  • For some reason the background color is not changing. I have just uploaded an image showing this problem. Any advice? – Rajat Khare Feb 10 '17 at 00:10
  • I can't see it in your code, but are you implementing the datasource methods of the tableview, aren't you? Could you upload the code of those methods? Specifically the one of cellForRowAt indexPath: IndexPath – Federico Ojeda Feb 10 '17 at 00:36
  • how do I implement the datasource methods of the tableview? – Rajat Khare Feb 10 '17 at 00:54
  • You are using a tableViewController, therefore the class is already the `dataSource` and the `delegate` of the tableView. You can override several methods that tells you tableView how to react to certain events or what information to give it. There are three importants. I'll edit the question so its better understood – Federico Ojeda Feb 10 '17 at 00:58
  • I have added my full code to the question above, if that will give you an idea as to how I can solve this. – Rajat Khare Feb 10 '17 at 00:58
  • I've updated the answer, check it out. There's a lot of methods that the tableview needs implemented actually. You should check out the source code for the UITableViewDataSource and the UITableViewDelegate for further info, or look for any similar tutorial. – Federico Ojeda Feb 10 '17 at 01:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135340/discussion-between-rajat-khare-and-federico-ojeda). – Rajat Khare Feb 10 '17 at 04:05
1

To change cell background color use this :

cell.contentView.backgroundColor = //Your Color 

Or

cell.backgroundColor = //Your Color 

Instead of tableview you should use cell to change color.

Hemant Solanki
  • 894
  • 10
  • 24
  • To change navigation bar color follow this link : http://stackoverflow.com/questions/39931463/swift-ios-change-the-color-of-a-navigation-bar – Hemant Solanki Feb 10 '17 at 00:47
  • @Rajat Khare Please review this tutorial and you will get complete idea about tableview implementation in iOS : http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/ – Hemant Solanki Feb 10 '17 at 01:26
  • If I could accept two answers I would, because both you and Frederico had helped me in achieving this. Just wanted to let you know. Thanks! – Rajat Khare Feb 11 '17 at 00:05
  • 1
    Thanks @RajatKhare – Hemant Solanki Feb 13 '17 at 19:49