After applying a search filter, we want the TableView to scroll to the top of the results. We have a TableView Header that scrolls with the content. So, top means that we want to see the TableView Header along with the first few cells. I found the following and tried a few solutions it had for iOS 10 and iOS 11: UITableView - scroll to the top However, we kept hitting bugs.
For example, we tried applying a scrollRectToVisible solution such as:
self.tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
However after applying the filter three times, the following gray area sometimes appeared above the TableView Header:
The scrollToView for the TableView Header solution in Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift has the same gray area effect.
One other approach also produced the gray area:
UIView.animate(withDuration: 0.25) {
self.tableView.setContentOffset(CGPoint.zero, animated: false)
}
So, we tried setContentOffset solutions such as this one:
if #available(iOS 11.0, *) {
tableView.beginUpdates()
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.adjustedContentInset.top), animated: true)
tableView.endUpdates()
} else {
tableView.beginUpdates()
tableView.setContentOffset(CGPoint(x: 0, y: -tableView.contentInset.top), animated: true)
tableView.endUpdates()
}
However, it seems that there is a scenario where it won't take the user all the way to the top such as here: https://youtu.be/OmWCk5PedJE I am not positive, but perhaps it has to do with the cell being under the top a little.
It should be noted that the TableView is a Grouped style so that the HeaderView will scroll up and away. We're using a large estimatedRowHeight (huge number of 800) and rowHeight is set to UITableViewAutomaticDimension.
The document outline in Interface Builder is:
Any idea of a solution that takes one to the top of the search results without having the side effect of the gray area above the TableView Header view or having it fail if the cell is scrolled to a certain point first? Thank you.