Get light or dark variant of a color declared in assets

↘锁芯ラ 提交于 2020-12-27 06:12:42

问题


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:

colors

Demo:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!