SwiftUI 未从设置中检测到暗模式

SwiftUI not detecting dark mode from setting

在我设备上的 SwiftUI 中,我将外观设置为深色模式。但是,我的应用程序不会根据该外观更改背景颜色。对于颜色资源,我设置了明暗外观的值,就像这样

在我的代码中,这是我设置背景颜色和静态变量的方式,

extension Color {
  static let backgroundColor = Color("Background")
}

..... // In my View 
        ZStack {
        Color.backgroundColor
            .ignoresSafeArea()
        ......
        }

还有我调试检查的时候

@Environment (\.colorScheme) var colorScheme:ColorScheme

returns 我的设备确实处于深色模式。知道发生了什么以及为什么我无法从用户的设备首选项中自动激活暗模式。

此代码在模拟器和设备上运行良好:

struct ColorSchemeView: View {
    
    var body: some View {
        Color.backgroundColor
            .ignoresSafeArea()
    }
}

fileprivate extension Color {
    static let backgroundColor = Color("DarkLight")
}

我怀疑您的代码中还有其他问题,需要真正的 Minimal Reproducible Example (MRE) 才能调试。

当你使用

.preferredColorScheme(.light | .dark)

它将偏好应用于整个视图,而不是 属性 正在使用的元素。