Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dark mode in SwiftUI Preview doesn't have a dark background with Xcode 11.4

Tags:

xcode

swiftui

Does anyone have the same problem, that Xcode (11.4) doesn't show a dark background, when previewing in dark mode?

Steps to reproduce:

1) Create a new project, a Single View App

2) Add the .environment-modifier to the preview:

Group {
    ContentView()
        .environment(\.colorScheme, .light)
    ContentView()
        .environment(\.colorScheme, .dark)
}

You get this result:

Xcode preview

like image 542
Lupurus Avatar asked Mar 29 '20 09:03

Lupurus


People also ask

How do I Preview dark mode in Xcode?

Xcode 11's Interface Builder also comes with a new feature for you to preview the screen in dark mode. Open any of your storyboards file, you should find the Interface Style option in the device menu. Click the dark mode icon to switch to the dark mode.

How do I support dark mode in SwiftUI?

SwiftUI lets us detect whether dark mode or light mode is currently enabled using the colorScheme environment key. If you declare this using @Environment , you can refer to it in your views and they will automatically be reloaded when the color scheme changes.

How do I create a preview in SwiftUI?

You can also create new preview structures in an existing SwiftUI view file by choosing Editor > Create Preview. For the complete list of preview customizations, see Previews in Xcode.


1 Answers

Setting \.colorScheme in the environment is deprecated, so instead use the .preferedColorScheme modifier. For example,

ContentView()
    .preferredColorScheme(.dark)
like image 131
Austin Conlon Avatar answered Oct 08 '22 19:10

Austin Conlon