Search code examples
iosswiftuitableviewuisearchbaruisearchcontroller

UISearchController Overlapping TableView's scrollview. Best Way to Get Around This?


Just finished following Ray Wenderlich's "UI Search Controller" tutorial in which I create a search bar with scope button items:

https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

I got it working no problem, with the small exception that the scope buttons overlap the first result of my tableview scrollview:Picture Here

In my app I'm using a tableView which is embedded inside a UIViewController, I'm guessing this may be the problem. I have "Adjusts Scroll View Insets" checked on my UIViewController, didn't solve the problem. Even tried unchecking it on the weird chance that may have been the problem, still not working.

I'm thinking the problem has to do with the fact that the searchBar is being added as a headerView of the tableView in the tutorial:

 tableView.tableHeaderView = searchController.searchBar

However, I have no idea why it wouldn't cause any problems for what looks like a UINavigationController, but it would cause problems for someone embedding it on a tableView within a UITableViewController.

Any help/workaround suggestions are much appreciated! Thank you so much in advance.

EDIT: Code instantiating search controller, as following the Wenderlich tutorial:

        searchController.searchBar.scopeButtonTitles = ["All","party", "hangout", "recreation", "event", "other"]
        searchController.searchBar.tintColor = UIColor().crimson()

        searchController.searchBar.delegate = self

        //MARK: Search controller initialization
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false

Solution

  • Answer discovered: Implement the heightForHeaderInSection method in tableView and return the height. Answer can be found here:

    https://stackoverflow.com/a/32996842/7389684