iOS Tutorials: TableView Setting Page Night Mode

OVERVIEW

The objective of this tutorial is to create a tableview that contains table view setting for night mode. Through the setting, user may turn on or off night mode and the changes will be reflected. You may implement as many setting as you like but for this, I will only be implementing night mode.

Begin by creating a new project by

  • Dragging Table View Controller onto Main.storyboard 
    • Change the setting of Content to Static Cells
    • Change the setting of Style to Group

  • Add Label & Switch into the cell. You may choose any many section or cells as you like. We’ll only be using one section here.

  • Add Bar Button Item and name it Settings and create a segue with a name of toSettings
    • Create an IBAction from this Bar Button Item that performSegue

In our SettingTableViewController,

  • We need to create an IBOutlet for the Switch to catch its state such as on/off
  • We will be using UserDefault here which will save the value of the switch
  • Create an IBAction for the Switch detecting the click
    • With this, when the switch is on, we will save the value of true into the key of night and vice versa
 

  • However if you notice, switch is always set to true every time we open the settings. We could bypass that with the following code
    • With the code below, every time we click into the setting page, the it will shows the value saved in the userDefault

We are done with the setting page, lets go to the front page.

  • Drag TextView into Main.storyboard 
  • We will also be using userDefault here
     
  • Same concept as previous steps, we will be retrieving the value that has been saved and see the value
    • If the value is true, that means it is night mode, we want the text to be white and the background to be black.
    • If the value is false, then it’s not night mode, we want the text to be black and the background to be white. 
 

There are definitely many different way to go about doing this. You dont have to do exactly what I do.

 

The codes are uploaded on GitHub

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*