So I have an basic Taskview:
TabView {
NavigationView {
TasksView()
}
.tabItem {
Image(systemName: "bookmark")
Text("Tasks")
}
NavigationView {
ShopView()
}
.tabItem {
Image(systemName: "cart.fill")
Text("Shop")
}
}
.environment(.colorScheme, isDarkMode ? .dark : .light)
And now I want to add an if statement to .environment. So like if Default: .environment(.colorScheme, isDarkMode ? .dark : .light)
How can I do this?
2
Answers
Use
@Environment
to call the current color scheme. Then you can create a color variable.Here’s a possible solution. Use an ObservableObject class, store and save the variable. Set it as a
StateObject
and set asenvironmentObject
in yourApp
, then you can handle updates in yourView
by accessing the variable.