O-mkar O-mkar - 3 months ago 30
iOS Question

UITableViewController hides navigationbar[Search Bar]

I Have added search bar to navigation bar title view and when I start searching the UITableViewController hides navigation bar how do I get UITableViewController below the navigation bar.


Before Searching


Before Searching


After Typing


After Typing


Code


let resultController = mainStoryboard.instantiateViewControllerWithIdentifier("SearchTableViewController") as! UITableViewController

self.searchController = UISearchController(searchResultsController: resultController)
// self.searchController.searchResultsUpdater = self
// self.searchController.delegate = self
// self.searchController.searchBar.delegate = self
self.searchController.searchBar.tintColor = UIColor.whiteColor()
self.searchController.searchBar.placeholder = "Search Outlets"
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = false

Answer

As far as I can tell your view controller does not have the definesPresentationContext property set. If you don't set this, strange things are bound to happen. Try updating your code like this:

let resultController = mainStoryboard.instantiateViewControllerWithIdentifier("SearchTableViewController") as! UITableViewController

self.searchController = UISearchController(searchResultsController: resultController)
//self.searchController.searchResultsUpdater = self
//self.searchController.delegate = self
//self.searchController.searchBar.delegate = self
self.searchController.searchBar.tintColor = UIColor.whiteColor()
self.searchController.searchBar.placeholder = "Search Outlets"
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = false

self.definesPresentationContext = true

The last line is the line I've added. When I tested this in one my own apps, not specifying this line caused the behaviour you're seeing.