How to scroll to top in IOS7 UITableView?

后端 未结 12 1792
粉色の甜心
粉色の甜心 2020-12-13 00:25

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

[tableView setContentOffset:CGPointZero animated:YES];

In IOS7 this

相关标签:
12条回答
  • 2020-12-13 00:40

    Try this:

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
    0 讨论(0)
  • 2020-12-13 00:40

    you can still use scrollToRowAtIndexPath: for the purpose

    0 讨论(0)
  • 2020-12-13 00:45

    Based on the accepted answer from @Markus Johansson, here is the Swift code 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);
        }
    }
    
    0 讨论(0)
  • 2020-12-13 00:46
    float systemVersion= [[[UIDevice currentDevice] systemVersion] floatValue];
    
    if(systemVersion >= 7.0f)
    {
      self.edgesForExtendedLayout=UIRectEdgeNone;   
    }
    

    Try this code in viewDidLoad() method.

    0 讨论(0)
  • 2020-12-13 00:46

    in swift I used:

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

    but @Alvin George's works great

    0 讨论(0)
  • 2020-12-13 00:48

    Swift 3

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

    self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: UITableViewScrollPosition.top, animated: false)
    
    0 讨论(0)
提交回复
热议问题