获取 light/dark 模式主题更改的更新
Get update for light/dark mode theme change
我有一个 SwiftUI 应用程序,其中有一个 SpriteKit 场景。但是,在浅色模式和深色模式之间切换时,场景背景在重新打开应用程序之前不会改变颜色。
这不太理想,所以我想在 light/dark 模式外观更改时注册。我资产中的颜色具有 "any" 和 "dark" 外观。
Code inside updateUIView()
within a UIViewRepresentable
struct, which sets up the scene when it is linked with SwiftUI:
scene.backgroundColor = UIColor(named: "Color.Scene")!
uiView.presentScene(scene)
如何实现?我正在使用 iOS 13 的系统宽暗模式,所以我不想要通知中心的解决方案,因为我只想根据系统设置更新背景。
您可以在视图中使用 @Environment (\.colorScheme) var colorScheme: ColorScheme
并使视图 body
对其作出反应:
struct ContentView: View {
@Environment (\.colorScheme) var colorScheme: ColorScheme
var body: some View {
HStack {
if self.colorScheme == .light {
// draw for light mode
} else {
// draw for dark mode
}
}
}
}
我有一个 SwiftUI 应用程序,其中有一个 SpriteKit 场景。但是,在浅色模式和深色模式之间切换时,场景背景在重新打开应用程序之前不会改变颜色。
这不太理想,所以我想在 light/dark 模式外观更改时注册。我资产中的颜色具有 "any" 和 "dark" 外观。
Code inside
updateUIView()
within aUIViewRepresentable
struct, which sets up the scene when it is linked with SwiftUI:
scene.backgroundColor = UIColor(named: "Color.Scene")!
uiView.presentScene(scene)
如何实现?我正在使用 iOS 13 的系统宽暗模式,所以我不想要通知中心的解决方案,因为我只想根据系统设置更新背景。
您可以在视图中使用 @Environment (\.colorScheme) var colorScheme: ColorScheme
并使视图 body
对其作出反应:
struct ContentView: View {
@Environment (\.colorScheme) var colorScheme: ColorScheme
var body: some View {
HStack {
if self.colorScheme == .light {
// draw for light mode
} else {
// draw for dark mode
}
}
}
}