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.

4

5 回答 5

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.

于 2012-12-09T04:03:50.587 回答
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)
于 2019-02-17T20:16:16.590 回答
3

Same thing for swift:

collectionView?.scrollToItemAtIndexPath(NSIndexPath(forItem: theData.count - 1, inSection: 0), atScrollPosition: .Right, animated: false)
于 2016-01-30T04:21:38.957 回答
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)
        }
    }
于 2021-05-08T20:05:55.573 回答
0

Use This Extention

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

}

于 2021-12-30T00:28:46.610 回答