Search code examples
swiftios10ios11

Search controller below navigation item prior to iOS 11


I am using this code with no problem, but I would like to place the search controller below the navigation item’s titleView, instead of replacing it. With iOS11 it’s as easy as setting navigationItem.searchController to the searchController, and it will place it below the titleView, but in the navigationItem.

Any ideas on how to do this prior to iOS 11, instead of replacing the titleView?

Current Code:

if #available(iOS 11, *) {
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
} else {
    navigationItem.titleView = searchController.searchBar
}

Solution

  • I don't know if it's still relevant for you, but for anyone searching an answer and ending up here, this is a way to do it:

    if #available(iOS 11.0, *) {
       navigationItem.searchController = searchController
    } else {
       tableView.tableHeaderView = searchController.searchBar
    }