ios – SwiftUI: Replace mum or dad view on change of UserDefaults in baby view


I’m making a sport with a minimal colour scheme. I retailer the colours that I exploit as computed static var‘s in an enum in order that I can name them in any view. I’m making an attempt to make a secondary colour scheme (colorblind). My code seems to be like this:

enum GameColors {
    static var exampleColor: Coloration {
        !UserDefaults.normal.bool(forKey: GamePrefs.colorBlindMode) ? Coloration.inexperienced : Coloration(purple: 0 / 255, inexperienced: 213 / 255, blue: 255 / 255)
    }
}
enum GamePrefs {
     static let colorBlindMode: String = "colorBlindMode"
}

My settings menu known as in my primary menu view like so:

struct MainMenuView: View {
    @State var settingsClicked: Bool = false

    var physique: some View {
        VStack {
            Button {
                settingsClicked.toggle()
            } label: {
                Textual content("Settings")
                .foregroundColor(GameColors.exampleColor)
            }
            if settingsClicked {
                SettingsView()
            }
        }
    }
}

struct SettingsView: View {
    @AppStorage(GamePrefs.colorBlindMode) var colorBlindMode = false
    var physique: some View {
        Toggle(isOn: $colorBlindMode) {
            Textual content("Coloration Blind Mode: (colorBlindMode ? "On" : "Off")")
        }
        .toggleStyle(SwitchToggleStyle(tint:GameColors.exampleColor))
    }
}

Once I toggle colorBlindMode, solely SettingsView‘s colours are up to date, the colour of the principle menu doesn’t change except I work together with it. What’s the best approach to get each to replace? Is there a approach to bind the @AppStorage property wrapper so I can move it from the principle menu view to the kid view?

Leave a Reply

Your email address will not be published.