Keep navigation bar title during search with Searchable
Asked Answered
M

2

12

Is it possible to not hide the Title in Navigation Bar in search mode using Searchable? Using the code below Title in Navigation Bar is hidden when Search Field is focused.

 NavigationView {
        }
        .searchable(text: $viewModel.searchText,
                    placement: .navigationBarDrawer(displayMode: .always),
                    prompt: Strings.selectCity)
    }

I would like to achieve something like this:

Like this

Metzler answered 13/4, 2022 at 11:36 Comment(1)
Apple did this in the Shortcuts app. I'd really like to know how they did that.Roo
C
2

From iOS 17.1 this is possible with searchPresentationToolbarBehavior(_:) API:

@State private var searchText = ""

List {
    // ... content
}
.searchable(text: $searchText)
.searchPresentationToolbarBehavior(.avoidHidingContent)

Clarkclarke answered 19/3, 2024 at 6:31 Comment(0)
E
0

I was looking for same answer but did not found a solution. So I have added TextFiled instead of .searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always) ) to:

HStack {
    Image(systemName: "magnifyingglass").foregroundColor(.gray)
    TextField("Search", text: $searchText)
        .font(Font.system(size: 20))
    }
Execrate answered 13/1, 2024 at 21:53 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.