How to scroll to top in IOS7 UITableView?

后端 未结 12 1835
粉色の甜心
粉色の甜心 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:33

    In iOS7, whole screen UITableView and UIScrollView components, by default, adjust content and scroll indicator insets to just make everything work. However, as you've noticed CGPointZero no longer represents the content offset that takes you to the visual "top".

    Use this instead:

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

    Here, you don't have to worry about if you have sections or rows. You also don't tell the Table View to target the first row, and then wonder why it didn't show all of your very tall table header view, etc.

提交回复
热议问题