4

I've noticed when setting the preferredColorScheme in my SwiftUI app the .alert and .confirmationDialog modifiers don't use the colorScheme. Also the launch screen doesn't use the colorScheme either. It seems to be using system defaults, is this normal behaviour?

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

swiftui colorscheme

I was also unable to get better results using UIKit's overrideUserInterfaceStyle. The launch screen is still using system defaults, unsure how to solve that in SwiftUI without a launch screen. Also the code becomes pretty ugly and much more extensive.

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

UIKit UserInterfaceStyle

cole
  • 1,039
  • 1
  • 8
  • 35
  • For launch screen use UILaunchScreen corresponding property values (https://developer.apple.com/documentation/bundleresources/information_property_list/uilaunchscreen) – Asperi Apr 03 '22 at 08:48
  • @Asperi my SwiftUI app doesn’t contain any UILaunchScreen. Also that doesn’t really answer the issue for alert or confirmation dialogs. – cole Apr 03 '22 at 08:50
  • The preferredColorScheme by documentation is applied for current presentation. Alerts/dialogs has different presentation. Probably it is a bug, probably not. "doesn’t contain any UILaunchScreen" - so system one is used (note: launch screen is created and shown *before* your content view with modifier even created). – Asperi Apr 03 '22 at 08:55
  • @Asperi I think I'm just going to use `UIKit` for this instead of `preferredColorScheme`. – cole Apr 03 '22 at 09:13
  • Check this https://stackoverflow.com/questions/56537855/is-it-possible-to-opt-out-of-dark-mode-on-ios-13 – Isaac Apr 23 '22 at 07:21

1 Answers1

3

I couldn't figure out a pure SwiftUI way, but I found a simple solution. Use UIKit, then handle the changes in SwiftUI. I just needed to use .overrideUserInterfaceStyle. As for the Launch Screen in SwiftUI, add a Background color key to the dictionary. Then add a Color setfrom your assets and set the Background color key value to the name of the asset created as a String.

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

Launch Screen

Color Set

cole
  • 1,039
  • 1
  • 8
  • 35