On xcode I work within a workspace where there is a basic project and a framework. All colors are defined within the framework. When I configure a label with the color assets of the framework from the basic project on storyboard, the color is taken correctly, but if I then switch to dark mode, the color does not change, it always remains the same.
setting textColorBlack in dark mode should appear white but it doesn’t work.
I don’t understand what the problem is.
2
Answers
Xib/Storyboard just preserves selected color name (visible at dev time from all spaces), but in run-time on color schema changes it cannot find it, because by default it searches by name in main bundle.
Instead we have to assign color programmatically specifying exact location of color, like
Tested with Xcode 13.4 / iOS 15.5
In our case we defined some extensions, that load the corresponding Colors from the asset catalog:
SwiftUI’s
Color
is loaded directly from the asset catalog, but UIKit’sUIColor
is instantiated from the loadedColor
. This causes the described problem, since theUIColor
won’t automatically update, when switching to dark mode. The solution, of course, is to also load theUIColor
directly from the asset catalog .