Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make UICollectionView scroll right to left?

The natural direction for a UICollectionView to scroll when set horizontally is from left to right. Is there any way to reverse this? The simpler the better.

like image 229
Rob Caraway Avatar asked Dec 09 '12 03:12

Rob Caraway


3 Answers

I'm not sure exactly what you mean -- if you set the scrolling to horizontal, it scrolls equally well, left and right. If you want it to start it from the right side, you can use this method:

[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.theData.count - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];

This assumes that you have 1 section, and the array populating the collection view is called theData.

like image 144
rdelmar Avatar answered Oct 12 '22 23:10

rdelmar


Swift4 solution in cellForItemAt collectionView function

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell = categoryBook.dequeueReusableCell(withReuseIdentifier: "HomeCategoryCell", for: indexPath) as! HomeCategoryCell
                collectionView.transform = CGAffineTransform(scaleX:-1,y: 1);

                
                cell.transform = CGAffineTransform(scaleX:-1,y: 1);
               }

but this solution in some cases did not work properly if it dose not you can use ColletctionView scrollToItem method and you can implement it after you reload the data .

        self.YourCollectionView.reloadData()
        self.YourCollectionView.scrollToItem(at: NSIndexPath(item: self.YourObjectListData.count - 1, section: 0) as IndexPath, at: .right, animated: false)
like image 34
Mohamed Ayed Avatar answered Oct 13 '22 00:10

Mohamed Ayed


Same thing for swift:

collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: theData.count - 1, inSection: 0), atScrollPosition: .Right, animated: false)
like image 45
DmitryoN Avatar answered Oct 13 '22 00:10

DmitryoN