Contents

openSettings

A Settings presentation action stored in a view’s environment.

Declaration

var openSettings: OpenSettingsAction { get }

Discussion

Use the openSettings environment value to get an OpenSettingsAction instance for a given Environment. Then call the instance to open a window. You call the instance directly because it defines a callAsFunction() method that Swift calls when you call the instance.

For example, you can define a button that opens the settings window to a particular tab:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        #if os(macOS)
        Settings {
            SettingsView()
        }
        #endif
    }
}

struct SettingsView: View {
    @AppStorage("selectedSettingsTab")
    private var selectedSettingsTab = SettingsTab.general

    var body: some View {
        TabView(selection: $selectedSettingsTab) {
            GeneralSettings()
            AdvancedSettings()
        }
    }
}

struct AdvancedSettingsButton: View {
    @AppStorage("selectedSettingsTab")
    private var selectedSettingsTab = SettingsTab.general

    @Environment(\.openSettings) private var openSettings

    var body: some View {
        Button("Open Advanced Settings…") {
            selectedSettingsTab = .advanced
            openSettings()
        }
    }
}

enum SettingsTab: Int {
    case general
    case advanced
}

See Also

Managing a settings window