Maintaining Color Theme Manager On iOS

Posted By : Aditya Kumar Sharma | 28-May-2018

In the tutorial, we will try to learn a flexible way of coding so that we can manage the theme colour from a single instance. As whenever our client wants to change the theme color of the whole app we can change them anytime, easily.

 

Let’s first know, What is UIAppearance? UIAppearance allows us to access the appearance proxy for the class.

 

 

Customizing the Navigation Bar
 

Let’s customize all instances of the navigation bar.

 

UINavigationBar.appearance().tintColor = UIColor.blue

 

Now we will make a global class for managing the colour of all instances of the navigation bar in the App.  Just create a NavThemeManager.swift file and paste following lines of code:

 

import UIKit

class NavThemeManager {
           
    var navbar = UINavigationBar.appearance()

    func applyTheme(themeBlack: Bool) {
        
        if themeBlack {
           navbar.barTintColor = UIColor.init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)            
           navbar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.white]

        }else {
           navbar.barTintColor = UIColor.init(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)            
           navbar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor.black]
        }

    }

}

 

Now we will use this, Go to appdelegate: `didFinishLaunchingWithOptions` method and paste following:

 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    NavThemeManager().applyTheme(themeBlack: true)
    return true
}

 

Run the code and we will find that all navigation bar is having black colour with title colour as white. Now just change the theme to white by:

 

NavThemeManager().applyTheme(themeBlack: false)

 

And on running, the navigation bar will change to white with the title as black in colour.

 

 

Customizing the Tab Bar

 

Let’s go through another example to customize the tab bar color from a global class. Just create a TabBarThemeManager.swift file and paste following lines of code:

import UIKit

class TabBarThemeManager {
           
    var tabBar = UITabBar.appearance()

    func applyTheme(themeBlack: Bool) {
        
        if themeBlack {
           UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)

        }else {
           UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)

        }
    }
}

In `didFinishLaunchingWithOptions` method and call following:

 

TabBarThemeManager().applyTheme(themeBlack: false)

 

It's very easy to handle the whole App theme colour from here.

 

Thanks.

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..