searchResultsController with a UITableViewController hides the navigation bar, how to prevent it?

Viewed 283

When I assign a TableViewController as the constructor for UISearchController, when the table is loaded, it hides the navigation bar (that includes title and search bar). How can I make it, so it doesn't hide it?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        configureNavigationItem()
    }
}

extension ViewController {

    private func configureNavigationItem() {
        let resultsController =  UITableViewController(style: .plain)
        navigationItem.searchController = UISearchController(searchResultsController: resultsController)
        navigationItem.hidesSearchBarWhenScrolling = false
    }
}

Initial Load

enter image description here

Tap Any Key

enter image description here

1 Answers

Found the answer:

definesPresentationContext = true

as in:

private func configureNavigationItem() {
    let resultsController =  UITableViewController(style: .plain)
    navigationItem.searchController = UISearchController(searchResultsController: resultsController)
    navigationItem.hidesSearchBarWhenScrolling = false
    **definesPresentationContext = true**
}
Related