UITableView 在 tableView 中使用 SearchController 滚动到顶部 header

UITableView Scroll to top with SearchController in tableView header

所以我有一个tableView。 header 是一个隐藏了导航控制器的 UISearchController。我希望当用户在搜索控制器上搜索或点击取消时,将该 tableView 滚动到顶部。

问题是

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

实际上滚动到 tableView 的顶部,在本例中,它是 SearchController 的 Y,所以此时我的单元格的一半在搜索控制器后面。

tableView.scrollRectToVisible(CGRectZero, animated: false)

具有相同的行为。

搜索后的例子:

遇到了同样的问题,我认为这是由于范围标题引起的,试试这个:)

self.searchController.searchBar.scopeButtonTitles = [NSArray array]

或者你可以设置顶部header高度

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44.0
 }   

你试过设置tableview的contentoffset吗

tableView.contentOffset = CGPointMake(0, searchBar.bounds.size.height);

因此,我通过重做所有内容并滚动来设法让它工作:

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