Skip to content

Commit b31f71d

Browse files
Update README.md
1 parent a12d03f commit b31f71d

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,74 @@ When the user switches the theme, apply it by assigning it to the `current` inst
123123
Theme.current = halloweenTheme
124124
```
125125

126+
**Handling Theme Changes**
127+
128+
When changing themes dynamically, you may need to **update the UI** to reflect the new theme. Below are approaches for handling this in different environments.
129+
130+
**SwiftUI**
131+
132+
For SwiftUI apps, you can use `ThemeChangeObserver` to automatically refresh views when the theme updates.
133+
134+
```swift
135+
@main
136+
struct Root: App {
137+
var body: some Scene {
138+
WindowGroup {
139+
ThemeChangeObserver {
140+
Content()
141+
}
142+
}
143+
}
144+
}
145+
```
146+
147+
We recommend using this helper in the root of your app to redraw everything at once.
148+
149+
**UIKit**
150+
151+
For UIKit apps, use the `observeThemeChange(_:)` method to update elements that depend on the properties from the library.
152+
153+
```swift
154+
override func viewDidLoad() {
155+
super.viewDidLoad()
156+
157+
style()
158+
159+
observeThemeChange { [weak self] in
160+
guard let self else { return }
161+
self.style()
162+
}
163+
}
164+
165+
func style() {
166+
view.backgroundColor = UniversalColor.background.uiColor
167+
}
168+
```
169+
170+
**Manually Handling Theme Changes**
171+
172+
If you are not using the built-in helpers, you can listen for theme change notifications and manually update the UI:
173+
174+
```swift
175+
NotificationCenter.default.addObserver(
176+
self,
177+
selector: #selector(handleThemeChange),
178+
name: Theme.didChangeThemeNotification,
179+
object: nil
180+
)
181+
182+
@objc private func handleThemeChange() {
183+
view.backgroundColor = UniversalColor.background.uiColor
184+
}
185+
```
186+
187+
Don't forget to remove the observer when the view is deallocated:
188+
```swift
189+
deinit {
190+
NotificationCenter.default.removeObserver(self, name: Theme.didChangeThemeNotification, object: nil)
191+
}
192+
```
193+
126194
**Extend Colors**
127195

128196
All colors from the theme can be used within the app. For example:

0 commit comments

Comments
 (0)