6

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

2
  • Maybe that helps you stackoverflow.com/questions/56591669/… Commented Mar 29, 2020 at 9:23
  • The current accepted answer is out of date, so I think mine should be the accepted one now. Commented Jan 26, 2021 at 0:39

3 Answers 3

22

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

ContentView()
    .preferredColorScheme(.dark)
Sign up to request clarification or add additional context in comments.

2 Comments

How have I not seen this before? I just went through a bunch of effect implementing my own custom function for adding a dark mode to a view.
This should be the selected answer. Everything else, assuming it works, is overkill; especially for a preview mode.
0

As m-reza-f mentioned in a similar question, this is a bug in Xcode (which is still active as this answer is posted).

I'll add that instead of wrapping your actual body code in a NavigationView, you can simply wrap the previews code in your PreviewProvider in a NavigationView instead, to achieve the same results:

struct ContentView_Previews: PreviewProvider {
     Group {
          NavigationView {
               ContentView()
                    .environment(\.colorScheme, .light)
          }

          NavigationView {
               ContentView()
                    .environment(\.colorScheme, .light)
          }
     }
}

1 Comment

Looks like Austin Conlon's answer is probably more appropriate since it actually solves the problem. First time I've seen it as a suggested solution!
-1

try this:

@available(iOS 13.0, *)
public struct DarkView<Content> : View where Content : View {
    var darkContent: Content
    var on: Bool
    public init(_ on: Bool, @ViewBuilder content: () -> Content) {
        self.darkContent = content()
        self.on = on
    }

    public var body: some View {
        ZStack {
            if on {
                Spacer()
                    .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                    .background(Color.black)
                    .edgesIgnoringSafeArea(.all)
                darkContent.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity).background(Color.black).colorScheme(.dark)
            } else {
                darkContent
            }
        }
    }
}

@available(iOS 13.0, *)
extension View {
    @available(iOS 13.0, *)
    public func darkModeFix(_ on: Bool = true) -> DarkView<Self> {
        DarkView(on) {
            self
        }
    }
}

and then

yourView()
.environment(\.colorScheme, .dark)
                    .darkModeFix()

1 Comment

Thanks! But why the var on: Bool? This property is completely useless ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.