Ios – How to scroll to top in IOS7 UITableView

iosios7uitableview

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

[tableView setContentOffset:CGPointZero animated:YES];

In IOS7 this doesn't work anymore. The table view isn't scrolled completely to the top (but almost).

Best Answer

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.