SwiftUI以编程方式更改颜色主题
我有。光明和黑暗的主题 在预览(MyContainer\u预览)中,我可以通过以下方式更改它们:SwiftUI以编程方式更改颜色主题,swiftui,Swiftui,我有。光明和黑暗的主题 在预览(MyContainer\u预览)中,我可以通过以下方式更改它们: ForEach([.light,.dark], id: \.self) { theme in Group { ... } .environment(\.colorScheme, theme) //This line } ... 如何动态更改应用程序主题(f.e.按钮操作)。
ForEach([.light,.dark], id: \.self) { theme in
Group {
...
}
.environment(\.colorScheme, theme) //This line
}
...
如何动态更改应用程序主题(f.e.按钮操作)。
我想在SceneDelegate中更改它:
let contentView = ContentView()
contentView.environment(\.colorScheme, .dark) //Not work
有人告诉我,如果你把代码放在NavagationView中,你可以使用你的
Enviroment
来更改方案
var body: some View {
NavigationView{
//Your view here
}}
let contentView=contentView()
contentView.environment(\.colorScheme.dark)//现在可以工作了
UIHostingController(rootView: ContentView().environment(\.colorScheme, .light)
struct ContentView: View {
var body: some View {
NavigationView{
Text("!")
}}}
反应性:
return NavigationView {
VStack(alignment: .trailing, spacing: 0) {
...
}
}
.environment(\.colorScheme, appState.options.theme == .light ? .light : .dark)
这个方案不是由iPhone用户控制的吗?我需要覆盖它。如果用户希望总是暗的或亮的。我已经回答了您的问题,如果我不希望我的视图位于导航视图中,该怎么办?