Maintaining a colour theme manager on iOS swift

Abhimuralidharan
2 min readMay 8, 2017

--

A guide on how to create a theme manager class in swift so that the code is flexible and the developer can change the theme colour anytime with ease.

Source & reference : Before going through this tutorial, please go through the UIAppearance link1, link2 and link3. It will give a brief understanding about UIAppearance protocol.

UIAppearance is a protocol that returns a proxy that will forward any configuration to instances of a particular class.

I went through so many articles on internet and stackoverflow and found one article on medium which I think will be easy to understand for everyone. The link to the article is given in the reference section of this article.

Creating a global class for managing the colours used in the applications is a must have coding style . The client will always be asking for more and more changes and it will be a headace for the developer to manage the entire theme color and reuse the application.

If you are creating an app template, then the themeManager class is a must.

Just create a swift file called ThemeManager.swift and paste the below code. Create an instance of this class and call the predefined colors everytime. The rest is upto the developer.

Usage:

In your appdelegate: didFinishLaunchingWithOptions method, apply the theme as required.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {// Override point for customization after application launch.ThemeManager.applyTheme(theme: .theme1)return true}

Then, in your viewcontroller,

import UIKitclass ViewController: UIViewController {let theme = ThemeManager.currentTheme()override func viewDidLoad() {super.viewDidLoad()self.view.backgroundColor = theme.backgroundColor}}

or use :

self.view.backgroundColor = ThemeManager.currentTheme().backgroundColor

Reference: Durul Dalkanat’s article ,apple docs .

Enjoy!!

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

--

--