Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to achieve dark mode using SwiftUI [duplicate]

struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.colorScheme, .dark)
    }
}

I am using the above code to achieve dark mode on my demo project but its not working.
Any help or insight would be really appreciated.

like image 498
Abdul Karim Avatar asked Jun 14 '19 05:06

Abdul Karim


1 Answers

This seems to be a bug in Xcode 11.0 beta. A temporary workaround is to wrap your content inside a NavigationView.

For example, the following code will not take effect in dark mode preview:

var body: some View {
  Text("Hello World")
}

But after wrapping the content in a NavigationView, dark mode preview works as expected:

var body: some View {
  NavigationView {
    Text("Hello World")
  }
}

Result:

enter image description here

like image 124
M Reza Avatar answered Nov 15 '22 20:11

M Reza