In my table view I have to scroll to the top. But I cannot guarantee that the first object is going to be section 0, row 0. May be that my table view will start from section
In Swift-3 :
self.tableView.setContentOffset(CGPoint.zero, animated: true)
Adding on to what's already been said, you can create a extension (Swift) or category (Objective C) to make this easier in the future:
Swift:
extension UITableView {
func scrollToTop(animated: Bool) {
setContentOffset(CGPointZero, animated: animated)
}
}
Any time you want to scroll any given tableView to the top you can call the following code:
tableView.scrollToTop(animated: true)
Since my tableView
is full of all kinds of insets, this was the only thing that worked well:
Swift 3
if tableView.numberOfSections > 0 && tableView.numberOfRows(inSection: 0) > 0 {
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}
Swift 2
if tableView.numberOfSections > 0 && tableView.numberOfRowsInSection(0) > 0 {
tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0), atScrollPosition: .Top, animated: true)
}
This was the only code snippet that worked for me
Swift 4:
tableView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true)
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
tableView.setContentOffset(CGPoint(x: 0, y: -70), animated: true)
P.S. 70 is the height of my header and table view cell
Here is a simple example of UIScrollView extension that you can use everywhere in your app.
1) First you should create enum
with possible scroll directions:
enum ScrollDirection {
case top, right, bottom, left
func contentOffsetWith(_ scrollView: UIScrollView) -> CGPoint {
var contentOffset = CGPoint.zero
switch self {
case .top:
contentOffset = CGPoint(x: 0, y: -scrollView.contentInset.top)
case .right:
contentOffset = CGPoint(x: scrollView.contentSize.width - scrollView.bounds.size.width, y: 0)
case .bottom:
contentOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
case .left:
contentOffset = CGPoint(x: -scrollView.contentInset.left, y: 0)
}
return contentOffset
}
}
2) Then add extension to UIScrollView:
extension UIScrollView {
func scrollTo(direction: ScrollDirection, animated: Bool = true) {
self.setContentOffset(direction.contentOffsetWith(self), animated: animated)
}
}
3) Thats it! Now you can use it:
myScrollView.scrollTo(.top, animated: false)
This scroll bind to the tableView's content size and it looks more natural than scroll to CGPoint.zero
UITableView is a subclass of UIScrollView, so you can also use:
[mainTableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
Or
[mainTableView setContentOffset:CGPointZero animated:YES];
And in Swift:
mainTableView.setContentOffset(CGPointZero, animated:true)
And in Swift 3 & above:
mainTableView.setContentOffset(.zero, animated: true)