Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchControllerDelegate - Search bar not visible in table header

My UITableViewController is conforming to the new UISearchControllerDelegate and also UISearchResultsUpdating.

Here is my setup code for the search bar:

override func viewDidLoad() {
    var searchController = UISearchController(searchResultsController: self)
    searchController.searchResultsUpdater = self
    self.tableView.tableHeaderView = searchController.searchBar
    self.definesPresentationContext = true
}

However, when running this in the simulator there is no search bar in the table header, even though it is specified in the code. I also tried this code in viewWillAppear, but again no search bar was shown.

like image 579
BytesGuy Avatar asked Jun 09 '14 12:06

BytesGuy


1 Answers

I was informed by an Apple Engineer that you must give the Search Bar a frame. If you print the frame of the search bar, you will notice it's height is zero. So this is probably a bug in Apple's code.

searchController.searchBar = CGRectMake(0.0, 0.0, 320.0, 44.0)

Edit:

The documentation specifies that you must pass in the View Controller that you want to display the results. To display this in the same View Controller you are in, pass in nil.

var searchController = UISearchController(searchResultsController: nil)
like image 196
Vikings Avatar answered Sep 28 '22 10:09

Vikings