I am trying to change the app's theme with 3 options: Default, Dark, Light. And after that the view jumps to the main view, but I would like to stay on the settings view.
struct SettingsView: View {
...
@State var colors = ["Alapértelmezett", "Sötét", "Világos"] //Default, Dark, Light
@AppStorage("colorIndex") var colorIndex: Int = 0
...
var body: some View {
...
Picker(selection: $colorIndex, label: Text("Megjelenés")) { //Color
ForEach(0 ..< colors.count) {
Text(self.[$0])
}
}
...
In "MyApp.swift":
@main
struct MyApp: App {
@AppStorage("colorIndex") var colorIndex: Int = 0
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
if colorIndex == 1 {
ContentView().environment(\.colorScheme, .dark)
}
else if colorIndex == 2 {
ContentView().environment(\.colorScheme, .light)
}
else {
ContentView()
}
}
}
}