I have .light and .dark theme.
In preview (MyContainer_Previews) I can change them by:
ForEach([.light,.dark], id: \.self) { theme in
Group {
...
}
.environment(\.colorScheme, theme) //This line
}
...
How can I change app theme on the fly (f.e. button action). I tri to change it in SceneDelegate:
let contentView = ContentView()
contentView.environment(\.colorScheme, .dark) //Not work
Reactive:
return NavigationView {
VStack(alignment: .trailing, spacing: 0) {
...
}
}
.environment(\.colorScheme, appState.options.theme == .light ? .light : .dark)
Some one told me if you put you code in NavagationView, you can use your enviorment
to change the scheme.
var body: some View {
NavigationView{
//Your view here
}}
let contentView = ContentView()
contentView.environment(\.colorScheme, .dark) //works now
UIHostingController(rootView: ContentView().environment(\.colorScheme, .light)
struct ContentView: View {
var body: some View {
NavigationView{
Text("!")
}}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With