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()
}
}
}
}
Why would you make the app responsible for the dark and light theme, I think you should let the system handle this. Other then that check your function. If you call ContentView it makes sense that it is shown.
And if you don't want the system to handle this. check this post How to switch programmatically to dark mode swift maybe this helps you