This guide explains how to manage Dark Mode and Light Mode in StarterAppKit, enabling you to set, detect, and dynamically toggle between themes seamlessly across your app.
Overview
StarterAppKit allows you to configure the app's color scheme globally through the Constants.swift file. Once the preferred color scheme is set, the entire app automatically adapts to this configuration.
Dark and Light mode functionality is powered by the ThemeManager, located in the ThemeManager.swift file. This utility provides:
Global Theme Control: Set the app-wide theme.
Dynamic Updates: Toggle between Dark Mode and Light Mode at runtime, and ensure your views update reactively.
Setting the App's Default Theme
To define the default theme for your app, navigate to the Constants.swift file and configure the color scheme (e.g., .dark or .light). StarterAppKit ensures all views adhere to this setting automatically.
Detecting the Current Theme
To determine the app's current theme within a view, follow these steps:
Reference the shared instance of the ThemeManager:
Access the currentTheme property to conditionally style your UI:
Example:
Here’s how to use the current theme to dynamically adjust a view's background color:
Result:
Whenever the theme changes, any view that references currentTheme will automatically update to reflect the new theme.
Changing the Theme at Runtime
StarterAppKit allows you to toggle between Dark Mode and Light Mode dynamically during runtime. This can be especially useful for implementing user-controlled theme switches.
Reference the shared instance of the ThemeManager:
Use the setTheme(_:) method to switch themes:
Example: Toggle Button for Theme Switching
Here’s an example of a toggle button to switch between themes:
Key Features of ThemeManager
Global Theme Management:
The ThemeManager centralizes theme handling, ensuring consistency across all views.
Reactive Updates:
Views automatically update their appearance when the theme changes.