Instance Method インスタンスメソッド

toolbar(id:content:)

Populates the toolbar or navigation bar with the specified items, allowing for user customization. ツールバーまたはナビゲーションバーにこの指定された項目を投入します、ユーザカスタマイゼーションを想定しています。

Declaration 宣言

func toolbar<Content>(id: String, content: () -> Content) -> some View where Content : CustomizableToolbarContent

Parameters パラメータ

id

A unique identifier for this toolbar. このツールバーに対するある固有な識別子。

content

The content representing the content of the toolbar. ツールバーの内容を表している内容。

Discussion 議論

Use this modifier when you want to allow the user to customize the components and layout of elements in the toolbar. The toolbar modifier expects a collection of toolbar items which you can provide either by supplying a collection views with each view wrapped in a ToolbarItem, or by providing a collection of views as a ToolbarItemGroup.

The example below creates a view that represents each ToolbarItem along with an ID that uniquely identifies the toolbar item to the customization editor:


struct ToolsEditorView: View {
    @State private var text = ""
    @State private var bold = false
    @State private var italic = false
    @State private var fontSize = 12.0


    var displayFont: Font {
        let font = Font.system(size: CGFloat(fontSize),
                               weight: bold == true ? .bold : .regular)
        return italic == true ? font.italic() : font
    }


    var body: some View {
        VStack {
            TextEditor(text: $text)
                .font(displayFont)
                .toolbar(id: "editingtools") {
                    ToolbarItem(id: "sizeSelector") {
                        Slider(
                            value: $fontSize,
                            in: 8...120,
                            minimumValueLabel:
                                Text("A").font(.system(size: 8)),
                            maximumValueLabel:
                                Text("A").font(.system(size: 16))
                        ) {
                            Text("Font Size (\(Int(fontSize)))")
                        }
                        .frame(width: 150)
                    }
                    ToolbarItem(id: "bold") {
                        Toggle(isOn: $bold) {
                            Image(systemName: "bold")
                        }
                    }
                    ToolbarItem(id: "italic") {
                        Toggle(isOn: $italic) {
                            Image(systemName: "italic")
                        }
                    }


                }
        }
        .navigationTitle("My Note")
    }
}

A window showing the macOS toolbar customization

In macOS you can enable menu support for toolbar customization by adding a ToolbarCommands instance to a scene using the commands(content:) scene modifier:


@main
struct ToolbarContent_macOSApp: App {
    var body: some Scene {
        WindowGroup {
            ToolsEditorView()
                .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
        .commands {
            ToolbarCommands()
        }
    }
}

When you add the toolbar commands, the system adds a menu command to your app’s top level menu to provide toolbar customization support. This is in addition to the ability to Control-click on the toolbar to open the toolbar customization editor.

A screenshot of the toolbar editor support for the macOS view

See Also 参照

Toolbars