Switching Themes in Swift

Add multiple themes to your app with the easiest way.

Creating a New Project

Creating a new Xcode project.
Fresh Xcode Project.
Fresh Xcode Project.
Fresh Xcode Project.

Designing the App

1-) Desing the main view controller.

Main view controller.

2-) Designing the “Select Theme” view controller.

Final result.

3-) Adding segue and linking storyboards elements.

Added segue.
Linking the main view controller elements.
Linking the select theme view controller elements.

Coding

Creating a Theme Protocol

protocol ThemeProtocol { 
}
protocol ThemeProtocol {
var buttonTintColor: UIColor { get set }
var textColor: UIColor { get set }
var textFont: UIFont { get set }
var authorTextFont: UIFont { get set }
var backgroundColor: UIColor { get set }
var backgroundImage: UIImage { get set }
}
The final version of ThemeProtocol swift file.

Creating Themes

struct ThemeName: ThemeProtocol {
var buttonTintColor: UIColor
var textColor: UIColor
var textFont: UIFont
var authorTextFont: UIFont
var backgroundColor: UIColor
var backgroundImage: UIImage
}
struct OriginalTheme: ThemeProtocol {
var buttonTintColor: UIColor = UIColor.black
var textColor: UIColor = UIColor.black
var textFont: UIFont = UIFont.systemFont(ofSize: 25)
var authorTextFont: UIFont = UIFont.systemFont(ofSize: 20)
var backgroundColor: UIColor = UIColor.systemBackground
var backgroundImage: UIImage = UIImage(named: "ClearImage")!
}
struct PurpleTheme: ThemeProtocol {
var buttonTintColor: UIColor = UIColor.white
var textColor: UIColor = UIColor.white
var textFont: UIFont = UIFont.systemFont(ofSize: 25)
var authorTextFont: UIFont = UIFont.systemFont(ofSize: 20)
var backgroundColor: UIColor = UIColor.systemPurple
// ClearImage is transparent image that I created.
var backgroundImage: UIImage = UIImage(named: "ClearImage")!
}
struct PatternTheme: ThemeProtocol {
var buttonTintColor: UIColor = UIColor.white
var textColor: UIColor = UIColor.white
var textFont: UIFont = UIFont(name: "Arial-Black", size: 25)!
var authorTextFont: UIFont = UIFont(name: "Arial-Black", size: 20)!
// If background image is not ClearImage then backgroundColor is not important.
var backgroundColor: UIColor = UIColor.systemBackground
var backgroundImage: UIImage = UIImage(named: "PatternImage")!
}
The final version of Themes swift file.

Creating a Theme Manager

class ThemeManager {
static var current: ThemeProtocol = OriginalTheme()
}
The final version of ThemeManager swift file.

Applying Theme

viewWillAppear function added.
override func viewWillAppear(_ animated: Bool) {
applyTheme()
}
func applyTheme() {
self.view.backgroundColor = ThemeManager.current.backgroundColor
backgroundImage.image = ThemeManager.current.backgroundImage quoteLabel.textColor = ThemeManager.current.textColor
quoteLabel.font = ThemeManager.current.textFont
authorLabel.textColor = ThemeManager.current.textColor
authorLabel.font = ThemeManager.current.authorTextFont
// "Brush" button
themeButton.tintColor = ThemeManager.current.buttonTintColor
}

Setting Up SelectTheme ViewController

Final result.
Table view numberOfRowsInSection and cellForRowAt functions.
SelectThemeCell swift file codes.

Theme Selecting

static let themes = [OriginalTheme(), PurpleTheme(), PatternTheme()] as! [ThemeProtocol]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

ThemeManager.current = ThemeManager.themes[indexPath.row]
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {    ThemeManager.current = ThemeManager.themes[indexPath.row]    NotificationCenter.default.post(name:   NSNotification.Name("ThemeUpdate"), object: nil)}
func catchNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(reloadTheme), name: NSNotification.Name("ThemeUpdate"), object: nil)
}@objc func reloadTheme() {
applyTheme()
}
Final result.
Simulator images.
Themes that we created.

iOS Developer. Swift Student Challenge Winner.