CollectionView scroll page up

I have one CollectionViewController and it has 10 cells in it.

But when I run the application, it displays the cell at the bottom, but I want to display it from the top cell in viewDidLoad.

I tried a lot of things, but that didn't work.

How to fix it?

+8
source share
7 answers

Add this line after reloading the collection.

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

Edit:

I just want to add one more thing: if the collectionView data source has ZERO elements or it is nil , then your application will not work on the code, and, possibly, it will crash.

Write down the condition for checking the availability of the data source or not!

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

Where dataArray is your data source.

+6
source

in swift 3: after reloadData ()

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

Add this piece of code after reloading the collection view:

quick

yourCollectionView.setContentOffset(CGPoint.zero, animated: true)

Objective-c

[yourCollectionView setContentOffset:CGPointZero animated:YES];

+2
source

Please try this

 [self.yourcollectionview scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionTop animated:NO]; 
0
source

Make the animated version false. Maybe some view animations are performed as part of the collection view animation.

 collectionView.scrollToItem(at: indexPath, at: .top, animated: false) 
0
source

His work is excellent ......

 collectionView.setContentOffset(.zero, animated: false) 
0
source

To display data from the top cell in collectionView, you can use one method

 [collectionView setScrollsToTop:YES]; 

Hope this helps you.

-7
source

All Articles