4

I have an issue in that when I push a new UIViewController with a custom search bar, it expands the height of the Title Bar, but when I dismiss the window, I believe the parent UIViewController did not manage to resize back the Title Bar, and thus it leaves a black gap as shown in the diagram here: Navigation Item with black gap

Here is the code for the custom search bar that I think is causing the problem: (Please note that I am aware in iOS 11 there is a new search bar implementation but I am trying to be backward compatible).

    let searchController = UISearchController(searchResultsController: nil)

    func configureSearchController() {

        // Initialize and perform a minimum configuration to the search controller.
//        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        self.definesPresentationContext = true
        searchController.searchBar.placeholder = "Search here..."
        searchController.searchBar.delegate = self
        searchController.searchBar.sizeToFit()


        searchController.searchBar.layer.cornerRadius = 15
        searchController.searchBar.clipsToBounds = true

        self.searchController.hidesNavigationBarDuringPresentation = false
        self.extendedLayoutIncludesOpaqueBars = true
        // Place the search bar view to the tableview headerview.
        navigationItem.titleView = searchController.searchBar

    }

Any clues why it is happening, I try to turn on flag to update constraints on the parent navigationController, but it is still not working. As shown here:

override func didMove(toParentViewController parent: UIViewController?) {
    if parent == nil {
        print("parent moved back")
        // self.parent?.navigationItem.titleView?.sizeToFit()
        // self.parent?.navigationController?.navigationItem.titleView?.sizeToFit()
        // self.parent?.navigationController?.navigationBar.setNeedsUpdateConstraints()
        self.parent?.navigationItem.titleView?.setNeedsUpdateConstraints()
    }
}
dickyj
  • 1,830
  • 1
  • 24
  • 41

0 Answers0