Search code examples
iosswiftswiftuiios-darkmode

SwiftUI: Dark mode not detected when testing on device


I'm am trying to achieve dark mode within my iOS application using SwiftUI: simple test would be to change the background colour.

I have set up my colour set as seen below: Colour Set in Assets

ContentView.swift:

import SwiftUI

struct ContentView : View {

  @EnvironmentObject var session: SessionStore

  func getUser () {
      session.listen()
  }

  var body: some View {
    Group {
      if (session.session != nil) {
        VStack {
            WelcomeView()
            .background(Color("bg"))
            .edgesIgnoringSafeArea(.all)
        }
      } else {
        VStack {
            SigninView().transition(.move(edge: .bottom))
        }.frame(maxHeight: .infinity)
            .background(Color("bg"))
            .edgesIgnoringSafeArea(.all)
      }
    }.animation(.spring())
    .onAppear(perform: getUser)
  }
    
}

This doesn't work. However, when forcing dark mode with .colorScheme(.dark) after .onAppear - it works.

When debugging with @Environment (\.colorScheme) var colorScheme:ColorScheme it returns light, even though my iPhone is set to Dark Mode.

Have I missed something?


Solution

  • I figured it out. Turns out User Interface Style was set to light in my Info.plist file - just delete it.

    User Interface Style