6

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.

Daniyar
  • 2,975
  • 2
  • 26
  • 39
Rob Caraway
  • 3,856
  • 3
  • 30
  • 37
  • Use transform: http://stackoverflow.com/questions/25598194/aligning-right-to-left-on-uicollectionview/27560079#27560079 – LK__ Dec 23 '14 at 06:19

5 Answers5

8

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Yes, my apologies for lack of clarification, but that is what I meant -- for it to start at the right side instead of left, and to have the items arranged the same say -- with index 0 as the first item. – Rob Caraway Dec 09 '12 at 05:26
  • @RobCaraway, If you want what was the item at index 0 to appear on the right, as the first item of a right to left format, I think you would have to reverse your array, or in the cellForItemAtIndexPath method, invert the reference to your array index by using (array.count - row -1) instead of row to supply the data to the cells. – rdelmar Dec 09 '12 at 06:20
  • It doesn't allow `paging` to work properly, look at `Mohamed Ayed` answer, his solution is the best – imike Jul 23 '19 at 08:57
  • it scrolls to the item but not the collectionview's edge (if it has padding) – Vyachaslav Gerchicov Jul 22 '20 at 12:51
4

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)
moahmed ayed
  • 636
  • 7
  • 10
3

Same thing for swift:

collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: theData.count - 1, inSection: 0), atScrollPosition: .Right, animated: false)
DmitryoN
  • 310
  • 2
  • 11
1

Use This Extention

extension UICollectionViewFlowLayout {
open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
    return true  //RETURN true if collection view needs to enable RTL
}

}

Islam Alshnawey
  • 692
  • 10
  • 15
0

I have found using xCode 12.4 with an app that targets iOS 12 that this there seems to be no need to load the items in a different order or do any transforms. The only issue has to do with the initial scroll position. So all I need to do to get things working in both RTL and LTR is the following:

collectionView.reloadData {
        if self.collectionView.effectiveUserInterfaceLayoutDirection == .rightToLeft {
            self.collectionView?.scrollToItem(at: IndexPath(row:0, section:0), at: .right, animated: false)
        }
    }
Andy Weinstein
  • 2,639
  • 3
  • 21
  • 32