marshy101
marshy101

Reputation: 594

UITableView setContentOffSet is causing unwanted scrolling to the top

I am simulating a pull to refresh when my view loads which fetches data from an api.

[refreshControl beginRefreshing];
CGPoint newOffset = CGPointMake(0, -refreshControl.frame.size.height);
[tableView setContentOffset:newOffset animated:YES];

when the data comes back it stops the refresh control, reloads the tableview and

 [refreshControl endRefreshing];   
 [_postsTableView setContentOffset:CGPointZero animated:YES];

If am at the top of the tableview fine, but if am in the middle or at the bottom it scrolls to the top undesirably.

Upvotes: 0

Views: 757

Answers (1)

colinbrash
colinbrash

Reputation: 481

Looks like you can just wrap that to avoid the undesired behavior:

if (_postsTableView.contentOffset.y < 0) {
    [_postsTableView setContentOffset:CGPointZero animated:YES];
}

Upvotes: 4

Related Questions