Why is it when I set UITableViewController navigationItem.searchController with UISearchController, navigation.searchController still is nil?

For some reason, when I set UITableViewController navigationItem.searchController with a UISearchController, it doesn’t take it.

Here is my code:

let searchController = UISearchController(searchResultsController: nil)

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

Here is the debug window:

? nil
! <UISearchController: 0x105077600>
? nil

My identical question here got an answer: swift - Why will UITableViewController navigationItem.searchController not set in iOS? - Stack Overflow.

This topic was automatically closed after 166 days. New replies are no longer allowed.