Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 541
Ivy Xing Avatar asked Nov 05 '17 05:11

Ivy Xing


Video Answer


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
like image 53
boa_in_samoa Avatar answered Oct 10 '22 09:10

boa_in_samoa