获取资产中声明的颜色的浅色或深色变体 [英] Get light or dark variant of a color declared in assets

查看:55
本文介绍了获取资产中声明的颜色的浅色或深色变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的资产中,我为浅色和深色外观声明了两种变体的主题颜色,效果很好.但是,我在应用程序中有一个特定的位置,无论是否启用了暗模式,都需要使用颜色的浅色变体.除了将相同的颜色声明为仅具有一个变体的单独颜色之外,还有其他方法可以在代码中获得该颜色吗?

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?

推荐答案

在SwiftUI中,如果需要在某些子视图中使用light变体,则足以为其强制指定 .colorScheme ,如下所示

In SwiftUI if there is need to use light variant for some subview it is enough to force specify .colorScheme for it, like below

颜色变体:

演示:

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
}

这篇关于获取资产中声明的颜色的浅色或深色变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆