In certain cases the app fails to use the correct asset appearances when manipulating the interface style programmatically in the app.
I’ve implemented dark mode in a SwiftUI app defining appearances for my assets – colors and images. The app looks for the device theme on launch, checking the colorScheme in the environment:
@Environment(.colorScheme) private var colorScheme: ColorScheme
Within the app users have the option to override this, choosing from light or dark mode with a switch. This setting is saved for future launches, and it’s used to set the theme like so:
@MainActor
class ThemeManager: NSObject, ObservableObject {
...
func setTheme(_ theme: Theme) {
(UIApplication.shared.connectedScenes.first as? UIWindowScene)?.windows.first!.overrideUserInterfaceStyle = theme.interfaceStyle
}
...
}
Which works perfectly well, as long as I don’t need to re-render a CALayer, that has colored sublayer components. My CALayer is first drawn part of the onAppear() lifecycle method of a view and all colors are using the correct appearance set by the user.
However, any time this layer has to be re-drawn while the view is still alive, the colors are appearing in the theme of the device instead of the one set in the app.
When the view is dismissed and the layer is killed, then reopened and rendered again, everything is back to normal.