CollectionView scroll to top CollectionView scroll to top xcode xcode

CollectionView scroll to top


in swift 3: after reloadData()

  self.collectionView.setContentOffset(CGPoint(x:0,y:0), animated: true)


Add this line after reloading the collection view.

[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO];

Edit:

Just want to add one more point here is if collectionView datasource has ZERO elements or it is nil then above code will not work and probably crash your app.

Write condition to check that datasource is available or not!

if (self.dataArray.count > 0) {    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO];}

Where, dataArray is your datasource.


Its work fine......

collectionView.setContentOffset(.zero, animated: false)