TechnologiesSwiftUI

ToolbarItem struct

iOSmacOStvOSwatchOSvisionOSiOS 14.0+✓ renders

A model that represents an item which can be placed in the toolbar

How it works

A ToolbarItem represents a single model item you place inside a toolbar modifier, pairing a piece of content with a placement that tells SwiftUI where in the interface chrome it belongs. Rather than positioning buttons and controls manually, you describe each one as a toolbar item and let the system resolve it to the correct location for the current platform and context — a navigation bar on iOS, a window toolbar on macOS. Reach for ToolbarItem when you need precise, semantic control over where an individual control lands, especially when distributing items across the leading and trailing edges of a navigation bar.

  1. Declare items inside the toolbar modifier

    ToolbarItem only has meaning within the closure passed to the .toolbar modifier, which collects the items and renders them into the surrounding chrome. In the example each ToolbarItem lives in the .toolbar { } attached to the Text("Inbox") content, and the enclosing NavigationStack plus .navigationTitle("Mail") provide the bar those items populate.

  2. Position the item with the placement parameter

    The placement parameter takes a ToolbarItemPlacement value that gives the item a semantic position rather than a fixed coordinate, so SwiftUI can adapt it per platform. The first item uses .topBarTrailing to sit at the trailing edge of the navigation bar, while the second uses .topBarLeading to anchor at the leading edge.

  3. Supply the content as a trailing closure

    Each ToolbarItem wraps a view-building closure that defines what the item actually shows, so any view — a button, an image, or a custom control — can become a toolbar item. Here one item's closure holds a Button whose label is an Image(systemName: "square.and.pencil"), and the other simply holds an Image(systemName: "line.3.horizontal").

  4. Combine multiple items in one toolbar

    Because ToolbarItem is a single discrete unit, you list several of them in the same .toolbar closure and SwiftUI lays each out according to its own placement. The example declares two independent items in sequence, letting the compose-button land on the trailing side and the menu glyph on the leading side without either affecting the other.

Try it — Change the second item's placement from .topBarLeading to .topBarTrailing and watch both controls collapse onto the trailing edge of the navigation bar.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

ToolbarItem.swift
struct ToolbarItemDemo: View {
    var body: some View {
        NavigationStack {
            Text("Inbox")
                .padding()
                .navigationTitle("Mail")
                .toolbar {
                    ToolbarItem(placement: .topBarTrailing) {
                        Button {
                        } label: {
                            Image(systemName: "square.and.pencil")
                        }
                    }
                    ToolbarItem(placement: .topBarLeading) {
                        Image(systemName: "line.3.horizontal")
                    }
                }
        }
    }
}
Live preview
Inb… 9:41 Mail
Inb… 9:41 Mail
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →