2

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?

Community
  • 1
  • 1
Ryan
  • 155
  • 1
  • 14

2 Answers2

3

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

User Interface Style

Ryan
  • 155
  • 1
  • 14
  • Thank you! I had that key set to Dark for some reason and it was driving me insane trying to figure out why Light mode wasn't working. – carl_h Oct 01 '21 at 09:03
2

Target iOS 14.0

After more than one day on this, I finally found a solution. If it can help someone out there ..

My app needs to get the system ColorScheme at app launch and then it will be handle by the user in app settings. And some Views are specifically force to .dark and some other to .light. User Interface Style wasn't set in Info.plist, neither Appearance.

Same issue : @Environment (\.colorScheme) var colorScheme:ColorScheme was returning always light ! What ever I was doing, or where ever the value was retrieved..

After testing all internet's solution, here it is :

UITraitCollection.current.userInterfaceStyle

This is the only way to get the correct phone dark mode setting in my case.

Hope it will avoid to loose to much time on this.

Grég
  • 291
  • 3
  • 6