Reset scroll on UICollectionView

2019-03-07 23:06发布

I have a horizontal UICollectionView which works fine and scrolls. When I tap an item I update my data and call reloadData. This works and the new data is displayed in the UICollectionView. The problem is the scroll position doesn't change and it is still viewing the last place. I want to reset the scrolling to the top (Or left in my case). How can I do this?

8条回答
爷的心禁止访问
2楼-- · 2019-03-07 23:56

CGPointZero in my case shifted the content of the collection view because you are not taking in count the content inset. This is what it worked for me:

    CGPoint topOffest = CGPointMake(0,-self.collectionView.contentInset.top);
    [self.collectionView setContentOffset:topOffest animated:YES];
查看更多
叛逆
3楼-- · 2019-03-08 00:05

You can use this method to scroll to any item you want:

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath 
               atScrollPosition:(UICollectionViewScrollPosition)scrollPosition 
                       animated:(BOOL)animated
查看更多
登录 后发表回答