问题
In my assets I have declared a theme color in two variants for both light and dark appearances, which works great. However, I have a specific place in the app where I need to use the light variant of the color regardless of whether dark mode is enabled or not. Is there any other way of getting that color in code, other than declaring the same color as a separate one with only a single variant?
回答1:
In SwiftUI if there is need to use light variant for some subview it is enough to force specify .colorScheme
for it, like below
Color variants:
Demo:
var body: some View {
VStack {
Rectangle().fill(Color("testColor"))
.frame(width: 100, height: 100)
.environment(\.colorScheme, .light) // << force light
}
.frame(width: 300, height: 300)
.background(Color("testColor")) // << system appearance
}
来源:https://stackoverflow.com/questions/60709874/get-light-or-dark-variant-of-a-color-declared-in-assets