问题描述
我正在尝试使用 SwiftUI 在我的 iOS 应用程序中实现暗模式:简单的测试是更改背景颜色.
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:
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)
}
}
这不起作用.但是,当在 .onAppear
之后使用 .colorScheme(.dark)
强制暗模式时 - 它可以工作.
This doesn't work. However, when forcing dark mode with .colorScheme(.dark)
after .onAppear
- it works.
当使用 @Environment (\.colorScheme) var colorScheme:ColorScheme
调试时,它返回 light,即使我的 iPhone 设置为暗模式.
When debugging with @Environment (\.colorScheme) var colorScheme:ColorScheme
it returns light, even though my iPhone is set to Dark Mode.
我错过了什么吗?
推荐答案
我想通了.原来用户界面样式在我的 Info.plist 文件中设置为 light
- 只需将其删除即可.
I figured it out. Turns out User Interface Style was set to light
in my Info.plist file - just delete it.
这篇关于SwiftUI:在设备上测试时未检测到暗模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!