TechnologiesSwiftUI

ToolbarDefaultItemKind struct

iOSmacOStvOSwatchOSvisionOSiOS 17.0+✓ renders

A kind of toolbar item a `View` adds by default.

How it works

ToolbarDefaultItemKind names the toolbar items that SwiftUI inserts on your behalf — controls like the sidebar toggle, the search field, and the navigation title that a container view adds automatically based on its context. Because these items are created by the framework rather than declared in your toolbar content, you need a way to refer to one of them when you want to opt out. ToolbarDefaultItemKind is that identifier: you pass a value of this type to the toolbar(removing:) modifier to suppress a default item you don't want. Reach for it when a system-provided control conflicts with your own layout or simply isn't wanted in a particular scene.

  1. Refer to a default item by its kind

    Each system-supplied control has a corresponding ToolbarDefaultItemKind value exposed as a type property, so you select one with leading-dot syntax instead of constructing it yourself. In the example, .sidebarToggle identifies the automatic sidebar-toggle button that SwiftUI would otherwise place in the toolbar.

  2. Remove it with toolbar(removing:)

    The toolbar(removing:) modifier takes a ToolbarDefaultItemKind and tells SwiftUI to leave that default item out of the rendered toolbar. Here .toolbar(removing: .sidebarToggle) strips the sidebar toggle while leaving the rest of the toolbar — and every item you declare explicitly — untouched.

  3. Apply it where the default items live

    Default toolbar items are generated by the surrounding navigation container, so the modifier belongs on a view inside that container. In the example it sits on the List within a NavigationStack, the same place where navigationTitle("Pieces") and searchable(text: $query) register their own toolbar contributions.

  4. Compose with other automatic items

    Removing one kind doesn't disturb the others, which lets a default control coexist with content the framework adds elsewhere. The searchable(text:) modifier still contributes its search field even though the .sidebarToggle kind has been removed, because each default item is addressed independently by its own ToolbarDefaultItemKind.

Try it — Delete the .toolbar(removing: .sidebarToggle) line and the sidebar-toggle button reappears in the toolbar, showing exactly which default item that ToolbarDefaultItemKind value controls.

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.

ToolbarDefaultItemKind.swift
struct ToolbarDefaultItemKindDemo: View {
    @State private var query = ""
    var body: some View {
        NavigationStack {
            List {
                Text("Bishop")
                Text("Knight")
                Text("Rook")
            }
            .navigationTitle("Pieces")
            .searchable(text: $query)
            .toolbar(removing: .sidebarToggle)
        }
        .padding()
    }
}
Live preview
Search Bishop Knight Rook 9:41 Pieces
Search Bishop Knight Rook 9:41 Pieces
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →