I have one CollectionViewController and have 10 cells in it.
But when I run the app it displays the cell at bottom, but I want to display it from top cell at viewDidLoad.
I tried many things but it didn't work.
How to fix it?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With