Search code examples
iosswiftuitableviewuisearchcontroller

UITableView Scroll to top with SearchController in tableView header


So I have a tableView. It's header is an UISearchController which hides the navigation controller. I want when the user searches or taps on Cancel on the search controller, to scroll that tableView to the top.

The problem is that

let top = NSIndexPath(forRow: 0, inSection: 0)
tableView.scrollToRowAtIndexPath(top, atScrollPosition: .Top, animated: false)

actually scrolls to the top of the tableView, which in this case, is the Y of the SearchController, so half my cell, is behind the search controller at this point.

tableView.scrollRectToVisible(CGRectZero, animated: false)

has the same behavior.

Example after search:

enter image description here


Solution

  • So, I managed to get it to work by redoing everything and scrolling with:

    func scrollToTableViewTop() {
        tableView.setContentOffset(CGPointZero, animated: false)
    }