SwiftUI 警告和确认对话框 preferredColorScheme

SwiftUI alert and confirmation dialog preferredColorScheme

我注意到在我的 SwiftUI 应用程序中设置 preferredColorScheme 时,.alert.confirmationDialog 修饰符不使用 colorScheme。此外,启动屏幕也不使用 colorScheme。它似乎在使用系统默认值,这是正常行为吗?

struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .preferredColorScheme(.light)
        }
    }
}

我也无法使用 UIKitoverrideUserInterfaceStyle 获得更好的结果。启动屏幕仍在使用系统默认值,不确定如何在 SwiftUI 没有启动屏幕的情况下解决这个问题。此外,代码变得非常丑陋并且更加广泛。

guard let scene = scenes.first as? UIWindowScene else { return }
scene.keyWindow?.overrideUserInterfaceStyle = .light // .light .dark .unspecified

我想不出 SwiftUI 方式,但我找到了一个简单的解决方案。使用 UIKit,然后在 SwiftUI 中处理更改。我只需要使用 .overrideUserInterfaceStyle。至于 SwiftUI 中的 Launch Screen ,在字典中添加一个 Background color 键。然后从您的资产中添加一个 Color set 并将 Background color 键值设置为创建为 String.

的资产的名称
let scenes = UIApplication.shared.connectedScenes
guard let scene = scenes.first as? UIWindowScene else { return nil }
scene.keyWindow?.overrideUserInterfaceStyle = .light //.dark, .light, .unspecified