iOS 11: Add other nav items next to a search bar embedded in navigation item title view

Viewed 352

In iOS 11, we now use

self.navigationItem.searchController = self.searchController

to embed search bar in navigation item title view. This, however, pushes the other navigation items above the search bar, like this:

enter image description here

Without using custom containers or going back to the old way of setting search bar, is there any way we can keep the other navigation items on the same level as the search bar in iOS 11? Like this:

enter image description here

1 Answers

You can try to achieve this by changing the cancel button like this:

let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.showsCancelButton = true
if let view = searchController.searchBar.subviews.first {
    for subview in view.subviews {
        if let myButton = subview as? UIButton {
            // setup button
            myButton.setTitle("", for: .normal)
            let image = UIImage(named: "myImage")
            myButton.setBackgroundImage(image, for: .normal)
        }
    }
}
navigationItem.searchController = searchController
Related