How do I add unified theming support in UIKit with Swift?

Adding unified theming support in UIKit using Swift allows for a consistent look and feel across your iOS applications. This can greatly enhance user experience and maintain brand identity.

To implement unified theming, you can utilize UserDefaults to store theme preferences and create a custom ThemeManager to handle styles dynamically.

// ThemeManager.swift import UIKit enum Theme: String { case light, dark var backgroundColor: UIColor { switch self { case .light: return UIColor.white case .dark: return UIColor.black } } var textColor: UIColor { switch self { case .light: return UIColor.black case .dark: return UIColor.white } } } class ThemeManager { static let shared = ThemeManager() var currentTheme: Theme { get { let themeString = UserDefaults.standard.string(forKey: "appTheme") ?? Theme.light.rawValue return Theme(rawValue: themeString) ?? .light } set { UserDefaults.standard.setValue(newValue.rawValue, forKey: "appTheme") updateAppearance() } } func updateAppearance() { let currentTheme = self.currentTheme UIApplication.shared.windows.forEach { window in window.backgroundColor = currentTheme.backgroundColor window.tintColor = currentTheme.textColor } } } // Usage // ThemeManager.shared.currentTheme = .dark // Set to dark theme // ThemeManager.shared.updateAppearance() // Apply theme change

unified theming UIKit Swift iOS themes