简体   繁体   English

SwiftUI App浅色模式如何使用深色模式颜色?

[英]How to use dark mode color in light mode of SwiftUI App?

I'm writing an app in SwiftUI, and have no trouble with normal UI.我正在 SwiftUI 中编写一个应用程序,正常 UI 没有问题。 However, when a view's background is opposite to the app background color (eg use picture background), I need exactly what that color in the other color scheme.但是,当视图的背景与应用程序的背景颜色相反时(例如使用图片背景),我需要的正是其他配色方案中的那种颜色。 For example, Apple Music's lyrics are always in dark mode behavior (white text color, and white background color when hover) no matter what system color scheme is.例如,无论系统配色方案是什么,Apple Music 的歌词始终处于深色模式行为(白色文本颜色,悬停时白色背景颜色)。

I know this Apple Music one can be solved by adding another color in Assets.xcassets , but it's just an example.我知道这个 Apple Music 可以通过在Assets.xcassets中添加另一种颜色来解决,但这只是一个例子。 What I need is to use light or dark of a color programatically.我需要的是以编程方式使用lightdark For those colors have different hex in different color schemes, how can I use its other side of color?对于那些 colors 在不同的配色方案中有不同的十六进制,我该如何使用它的另一面颜色? Say, can I use the dark mode one of Color.primary in light mode?比如说,我可以在亮模式下使用Color.primary的暗模式之一吗?

I tried a lot solutions, but in vain.我尝试了很多解决方案,但都是徒劳的。 Firstly, I tried to attach .preferredColorScheme() to that view, but it eventually change color scheme of my whole app.首先,我尝试将.preferredColorScheme()附加到该视图,但它最终改变了我整个应用程序的配色方案。 Secondarily, I tried to convert a color into NSColor or UIColor , but there also no method defined in those two frameworks, at least I couldn't find.其次,我尝试将颜色转换为NSColorUIColor ,但这两个框架中也没有定义方法,至少我找不到。 They all cannot work.他们都不能工作。 How can I achieve this?我怎样才能做到这一点?

VStack {
    Text("Hello")      // where I want to use dark mode color (white one)
        .foregroundColor(.primary)
}
.background(Color.brown)
.preferredColorScheme(.dark)  // I tried here

I'm not sure if there is a way to do this with SwiftUI.Color directly but we have an extension in our app like...我不确定是否有办法直接使用SwiftUI.Color执行此操作,但我们的应用程序中有一个扩展,例如...

extension UIColor {
    public var lightVariant: UIColor {
        resolvedColor(with: UITraitCollection(userInterfaceStyle: .light))
    }
    public var darkVariant: UIColor {
        resolvedColor(with: UITraitCollection(userInterfaceStyle: .dark))
    }
}

And then you could use it with something like...然后你可以将它与类似...

extension UIColor {
    public var asColor: Color {
        Color(self)
    }
}

To do...去做...

UIColor(named: "primary").darkVariant.asColor

Or something like that.或类似的东西。

Or from a Color to begin with...或者从Color开始...

UIColor(Color.primary).darkVariant.asColor

(You could create conveniences for this also.) (您也可以为此创造便利。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得 SwiftUI 颜色的明暗模式版本? - How to get the light or dark mode version of a SwiftUI Color? 从 SwiftUI 颜色获取动态 UIColor(明暗模式) - Getting Dynamic UIColor from SwiftUI Color (light and dark mode) 如何使用 SwiftUI 为明暗模式之间的过渡设置动画? - How to animate the transition between light and dark mode using SwiftUI? SwiftUI 使用@AppStorage 更改暗/亮模式 - SwiftUI dark / light mode change with @AppStorage 如何使用我的应用程序中使用的自定义颜色轻松支持明暗模式? - How do I easily support light and dark mode with a custom color used in my app? 如何根据暗/亮模式设置默认 label 颜色(在 Swift 中) - How to set default label color based on dark/light mode (in Swift) 在不使用设备设置的情况下将应用程序设置为具有亮/暗模式 - Setting app to have light/dark mode without the use of the device setting 除了暗模式和亮模式之外,Swift/SwiftUI 中的其他配色方案 - Additional ColorScheme in Swift/SwiftUI beside Dark Mode and Light Mode 如何在深色模式下使用 Light UIUserInterfaceStyle 图像版本? - How to use Light UIUserInterfaceStyle image version in Dark Mode? 如何强制查看 controller 在 macOS 中使用浅色或深色模式? - How to force a view controller to use light or dark mode in macOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM