Search bar in navigation bar displayed as icon

Hi,

I have a controlled contained in a split view controller primary controller with a UISearchController assigned in navigationItem.searchController.

On iPhone the search bar is directly displayed in the navigation bar. This is want I want.

But on iPad, the search bar is hidden and a search button is displayed in the top right of the navigation bar. In order to display the search bar the user must click on the icon to display the search bar.

I want the search bar to always be visible, like on iPhone. How can I achieve that?

This is the code I use to include the UISearchController:

let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false

navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false

And this is the result on both devices:

Thank you

Answered by DaleOne in 762643022

Just found that this is due to the iOS 16 preferredSearchBarPlacement feature. In order to always display the search bar we need to set this property to stacked.

if #available(iOS 16.0, *) {
    navigationItem.preferredSearchBarPlacement = .stacked
}
Accepted Answer

Just found that this is due to the iOS 16 preferredSearchBarPlacement feature. In order to always display the search bar we need to set this property to stacked.

if #available(iOS 16.0, *) {
    navigationItem.preferredSearchBarPlacement = .stacked
}
Search bar in navigation bar displayed as icon
 
 
Q