TechnologiesSwiftUISearch and Find

SearchPresentationToolbarBehavior struct

iOSmacOStvOSwatchOSvisionOSiOS 17.1+✓ renders

A type that defines how the toolbar behaves when presenting search.

How it works

SearchPresentationToolbarBehavior is a set of constants that tells SwiftUI how entering a search presentation should affect the surrounding toolbar. By default, when a search field becomes active, SwiftUI may hide toolbar content to give the search interface room — which can be jarring if that content is still relevant while the person is searching. Reach for SearchPresentationToolbarBehavior when you want explicit control over whether the toolbar collapses during search, applying it through the searchPresentationToolbarBehavior(_:) modifier on a searchable view hierarchy.

  1. Make the view searchable with searchable(text:)

    The behavior only has meaning where a search field exists, so it attaches to a hierarchy that has already opted into search. Here the List is bound to a query through .searchable(text: $query), with query declared as @State private var query = "", establishing the search presentation whose toolbar treatment you then customize.

  2. Apply searchPresentationToolbarBehavior(_:)

    This modifier takes a SearchPresentationToolbarBehavior value and propagates it to the search presentation in the enclosing context. In the example it is chained directly after the searchable declaration as .searchPresentationToolbarBehavior(.avoidHidingContent), scoping the chosen behavior to this NavigationStack and its toolbar.

  3. Choose the .avoidHidingContent constant

    SearchPresentationToolbarBehavior exposes its options as type properties, and .avoidHidingContent instructs SwiftUI to keep existing toolbar content visible rather than hiding it when search becomes active. This is the value passed in the example, so toolbar items remain on screen while the person types into the search field instead of being collapsed away.

  4. Let the navigation container supply the toolbar context

    The behavior governs a toolbar, so it needs a host that provides one. The NavigationStack plus .navigationTitle("Fruits") give the search presentation a navigation bar to act on, which is the surface SearchPresentationToolbarBehavior decides whether to preserve or hide.

Try it — Swap .searchPresentationToolbarBehavior(.avoidHidingContent) for .searchPresentationToolbarBehavior(.automatic) and activate the search field to watch the toolbar revert to SwiftUI's default hiding behavior.

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.

SearchPresentationToolbarBehavior.swift
struct SearchPresentationToolbarBehaviorDemo: View {
    @State private var query = ""
    let fruits = ["Apple", "Banana", "Cherry", "Mango"]

    var body: some View {
        NavigationStack {
            List(fruits.filter { query.isEmpty || $0.localizedCaseInsensitiveContains(query) }, id: \.self) { fruit in
                Text(fruit)
            }
            .navigationTitle("Fruits")
            .searchable(text: $query)
            .searchPresentationToolbarBehavior(.avoidHidingContent)
        }
    }
}
Live preview
Search fruit 9:41 Fruits
Search fruit 9:41 Fruits
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →