SwiftUI: Dark mode not detected when testing on device

[亡魂溺海] 提交于 2021-01-28 11:12:41

问题


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:

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?


回答1:


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



来源:https://stackoverflow.com/questions/59845579/swiftui-dark-mode-not-detected-when-testing-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!