Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll UICollectionView to the bottom programatically?

I have a UICollectionView. I want to scroll to the bottom when the view appears. How to do this?

Can someone help me out on this?. Thanks in advance.

like image 463
Sheik_101 Avatar asked May 11 '15 10:05

Sheik_101


2 Answers

You can use the below code to scroll a UICollectionView programmatically to the bottom

NSInteger section = [self numberOfSectionsInCollectionView:collectionView] - 1;
NSInteger item = [self collectionView:collectionView numberOfItemsInSection:section] - 1;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
[collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
like image 77
Sanjay Mohnani Avatar answered Nov 01 '22 12:11

Sanjay Mohnani


NSIndexPath *lastItem = /*Indexpath of last item*/;
[self.collectionView scrollToItemAtIndexPath:lastItem atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
like image 1
Aneeq Anwar Avatar answered Nov 01 '22 11:11

Aneeq Anwar