跳转至
Structure

ToolbarItem

A model to represent a navigation item.

Declaration

struct ToolbarItem<ID, Content> : ToolbarContent where Content : View

Overview

A model that represents a toolbar or navigation item.

A ToolbarItem is essentially the following structure:

struct ToolbarItem<ID, Content: View> {
    let id: ID
    let placement: ToolbarItemPlacement
    let content: Content
}
* id is responsible for efficient updates to the toolbar item. * placement controls where the item is placed. * content represents the actual content of the item.

Adding toolbar items

struct ExampleView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!")
                .toolbar {
                    ToolbarItem(id: "bananas") {
                        Text("🍌🍌")
                    }
                }
        }
    }
}
A navigation view displaying the text "Hello, World!" and a toolbar along the top edge of the frame containing the right-aligned item "🍌🍌".

Placing a toolbar item on the navigation bar

ToolbarItem can be explicitly placed on the navigation bar using either navigationBarLeading or navigationBarTrailing. For example:

struct ExampleView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!")
                .toolbar {
                    ToolbarItem(id: "bananas", placement: .navigationBarLeading) {
                        Text("🍌🍌")
                        Spacer()
                    }
                }
        }
    }
}
A navigation view displaying the text "Hello, World!" and a toolbar along the top edge of the frame containing the leading / left-aligned item "🍌🍌".

Availability

iOS 14.0+

macOS 11.0+

tvOS 14.0+

watchOS 7.0+

Topics


Instance Property

id The stable identity of the entity associated with this instance.


Initializer

init(id:placement:showsByDefault:content:) Creates a toolbar item with the specified placement and content, which allows for user customization.

init(placement:content:) Creates a toolbar item with the specified placement and content.


Type Alias

Body The type of content representing the body of this toolbar content.