UICollectionView - Scrolling to first cell

It might be possible to use the UIScrollView related property

collectionView.contentOffset.x = 0

The following solution works fine for me:

UIView.animate(withDuration: 0.5, animations: {
     self.collectionView?.contentOffset.x = 0
})

It scrolls to the first item and the contentInset can be seen as well.


Use this delegate method, setting the indexPath at the first position:

Swift

self.collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: 0, inSection: 0), 
                                                atScrollPosition: .Top, 
                                                        animated: true)

Swift 3

self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), 
                                  at: .top, 
                            animated: true)

Objective-C

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] 
                            atScrollPosition:UICollectionViewScrollPositionTop
                                    animated:YES];

Change UICollectionViewScrollPositionTop if you want to scroll and stop at a different position


You can use this for Objective - C

        [self.restaurantCollectionView setContentOffset:CGPointZero animated:YES];