Reset scroll on UICollectionView Reset scroll on UICollectionView objective-c objective-c

Reset scroll on UICollectionView


You want setContentOffset:. It takes a CGPoint as and argument that you can set to what ever you want using CGPointMake, but if you wish to return to the very beginning of the collection, you can simply use CGPointZero.

[collectionView setContentOffset:CGPointZero animated:YES];


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

- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath                atScrollPosition:(UICollectionViewScrollPosition)scrollPosition                        animated:(BOOL)animated


I use this quite often in different parts of my app so I just extended UIScrollView so it can be used on any scroll view and scroll view subclass:

extension UIScrollView {            /// Sets content offset to the top.    func resetScrollPositionToTop() {        self.contentOffset = CGPoint(x: -contentInset.left, y: -contentInset.top)    }}

So whenever I need to reset the position:

self.collectionView.resetScrollPositionToTop()