Structure

Settings

A scene that presents an interface for viewing and modifying an app’s settings. あるシーン、それはあるインターフェイスをアプリのもつ設定を検分するそして修正するために提示します。

Declaration 宣言

struct Settings<Content> where Content : View

Overview 概要

Use a settings scene to have SwiftUI manage views with controls for your app’s settings when you declare your app using the App protocol. When you use an App declaration for multiple platforms, compile the settings scene only in macOS:


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

Passing a view as the argument to a settings scene in the App declaration causes SwiftUI to enable the app’s Preferences menu item. SwiftUI manages displaying and removing the settings view when the user selects the Preferences item from the application menu or the equivalent keyboard shortcut:

A screenshot of the MyApp application menu, showing the active

The contents of your settings view are controls that modify bindings to UserDefaults values that SwiftUI manages using the AppStorage property wrapper:


struct GeneralSettingsView: View {
    @AppStorage("showPreview") private var showPreview = true
    @AppStorage("fontSize") private var fontSize = 12.0


    var body: some View {
        Form {
            Toggle("Show Previews", isOn: $showPreview)
            Slider(value: $fontSize, in: 9...96) {
                Text("Font Size (\(fontSize, specifier: "%.0f") pts)")
            }
        }
        .padding(20)
        .frame(width: 350, height: 100)
    }
}

You can define your settings in a single view, or you can use a TabView to group settings into different collections:


struct SettingsView: View {
    private enum Tabs: Hashable {
        case general, advanced
    }
    var body: some View {
        TabView {
            GeneralSettingsView()
                .tabItem {
                    Label("General", systemImage: "gear")
                }
                .tag(Tabs.general)
            AdvancedSettingsView()
                .tabItem {
                    Label("Advanced", systemImage: "star")
                }
                .tag(Tabs.advanced)
        }
        .padding(20)
        .frame(width: 375, height: 150)
    }
}

A screenshot showing a tabbed application settings view containing a

Topics 話題

Creating a Settings Scene

Supporting Types 支援を行う型

Default Implementations 省略時実装

Relationships 関係

Conforms To 次に準拠

See Also 参照

Secondary Scenes