How to scroll up in iOS7 UITableView?

In IOS6, I have the following code to scroll to the top of a UITableView

[tableView setContentOffset:CGPointZero animated:YES]; 

In iOS7, this no longer works. The view of the table does not scroll all the way up (but almost).

+43
ios uitableview ios7
Oct 08 '13 at 8:52
source share
12 answers

With some other answers here, I managed to get it to work. To avoid a crash, I must first check if there are any partitions. NsNotFound can be used as a row index if there are no rows in the first section. We hope that this should be a common function that needs to be placed in the UITableViewController:

 -(void) scrollToTop { if ([self numberOfSectionsInTableView:self.tableView] > 0) { NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0]; [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES]; } } 
+15
Oct 08 '13 at 9:22
source share

In iOS7, all UITableView and UIScrollView components in full screen by default adjust the contents and scroll through the indicator inserts to make everything work. However, as you noticed, CGPointZero no longer represents a content bias that brings you to the visual top.

Use this instead:

 self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top); 

Here you do not need to worry about whether you have sections or lines. You also don’t point to Table View targeting the first row, and then wonder why it doesn’t display your entire tall table header, etc.

+126
Nov 15 '13 at 22:24
source share

Try the following:

 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
+23
08 Oct '13 at 8:55
source share

Based on the accepted answer from @Markus Johansson, here is the Swift version:

 func scrollToTop() { if (self.tableView.numberOfSections > 0 ) { let top = NSIndexPath(row: Foundation.NSNotFound, section: 0) self.tableView.scrollToRow(at: top as IndexPath, at: .top, animated: true); } } 
+21
Aug 01 '14 at
source share
 var indexPath = NSIndexPath(forRow: 0, inSection: 0) self.sampleTableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true) 

or

 self.sampleTableView.setContentOffset(CGPoint.zero, animated:false) 
+4
Sep 24 '15 at 14:32
source share
 float systemVersion= [[[UIDevice currentDevice] systemVersion] floatValue]; if(systemVersion >= 7.0f) { self.edgesForExtendedLayout=UIRectEdgeNone; } 

Try this code in the viewDidLoad() method.

+3
Oct 08 '13 at 8:54
source share

Swift 3

If you have table view headers, CGPointZero may not work for you, but it always does the trick to scroll up.

 self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false) 
+3
Oct 22 '16 at 5:41
source share

you can use scrollToRowAtIndexPath: for this purpose

+2
Oct 08 '13 at 8:55
source share

I understand that this was answered, but I just wanted to give one more option:

 CGRect frame = {{0, 0},{1, 1}}; [self.tableView scrollRectToVisible:frame animated:YES]; 

This always ensures that the UITableView will scroll up. Accepted answer:

 NSIndexPath* top = [NSIndexPath indexPathForRow:NSNotFound inSection:0]; [self.tableView scrollToRowAtIndexPath:top atScrollPosition:UITableViewScrollPositionTop animated:YES]; 

didn't work for me because I was scrolling in the tableHeaderView, not the cell.

Using scrollRectToVisible works with iOS 6 and 7.

+2
Jun 18 '14 at 17:31
source share

Here is the idStar answer in the updated Swift 3 syntax:

 self.tableView.contentOffset = CGPoint(x: 0, y: 0 - self.tableView.contentInset.top) 

With animation:

 self.tableView.setContentOffset(CGPoint(x: 0, y: 0 - self.tableView.contentInset.top), animated: true) 
+2
Oct 14 '16 at 21:49
source share

in swift i used:

 self.tableView.setContentOffset(CGPointMake(0, 0), animated: true) 

but @ alvin george works great

0
Apr 13 '16 at 9:24
source share

Swift 4 UITableViewExtension:

 func scrollToTop(animated: Bool) { if numberOfSections > 0 { let topIndexPath = IndexPath(row: NSNotFound, section: 0) scrollToRow(at: topIndexPath, at: .top, animated: animated) } } 
0
Sep 22 '17 at 9:37 on
source share



All Articles