This is once again about that Lunch Card app.
There's a section in my app's settings that controls the appearance - color scheme, if you will - of the whole app. The three options are
Here's the code for where the picker is:
There's a section in my app's settings that controls the appearance - color scheme, if you will - of the whole app. The three options are
System Default
Light
Dark
Here's the code for where the picker is:
Code Block struct SettingsView: View { @State private var selectedAppearance = 1 var body: some View { // ... Picker(selection: $selectedAppearance, label: Text("Appearance")) { Button(action: { // Change app color scheme to be auto }) { Text("System Default") }.tag(1) Button(action: { // Change app color scheme to be light }) { Text("Light") }.tag(2) Button(action: { // Change app color scheme to be dark }) { Text("Dark") }.tag(3) } // ... }
Thanks for showing your code. Seems onAppear is called more times than expected with your view structure.
Please try this.
Please try this.
SettingsView:
Code Block import SwiftUI struct SettingsView: View { @State private var selectedAppearance = 1 @Binding var colorScheme: ColorScheme? var body: some View { NavigationView { Form { Section(header: Text("Customization")) { Picker(selection: $selectedAppearance, label: Text("Appearance")) { Text("System Default").tag(1) Text("Light").tag(2) Text("Dark").tag(3) } .onChange(of: selectedAppearance) { value in print(selectedAppearance) switch selectedAppearance { case 1: //print("System Default") colorScheme = nil case 2: //print("Light") colorScheme = .light case 3: //print("Dark") colorScheme = .dark default: break } } //... } //... } } //<- Closing `NavigationView` .onAppear { switch colorScheme { case .none: selectedAppearance = 1 case .light: selectedAppearance = 2 case .dark: selectedAppearance = 3 default: break } } } }