Search code examples
iosuitableviewuisearchbar

Empty space at the bottom of UITableView after adding UISearchBar


I'm making a UITableView that displays a record of past events. It all looked great until I added a search bar at the top. It all looks how I want when it's scrolled to the top:

enter image description here

However, when I scroll to the bottom there's now a bunch of white space. It's hard to tell in this screenshot as there's no screen border but the tableview scroll will rest on this screen with about half the page blank:

enter image description here

Interestingly, when I activate the search bar at all (i.e. click in it and then press cancel) the white space goes away until this view appears again.

I've tried all sorts of settings for the tableView as well as using reloadData() at various stages of the view layout process to no avail. I know the number of rows is correct. The only thing I've found to make the white space go away is to disable the search bar. But I want the search bar!

Here's the code for the search bar:

self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.placeholder = "Search by keyword, name, etc."
        controller.searchBar.barTintColor = UIColor(red: 224/256, green: 244/256, blue: 255/256, alpha: 1)
        controller.searchBar.delegate = self

        self.pastServicesTable.tableHeaderView = controller.searchBar

        return controller
    })()

Solution

  • Same thing happened to me. Stating the the tableview row height in the viewDidLoad (ie. self.tableView.rowHeight = 350) fixed it.