Aligning right to left on UICollectionView

前端 未结 13 626
悲哀的现实
悲哀的现实 2020-12-02 08:31

this is a pretty straightforward question, but I haven\'t been able to find a definitive answer to it on SO (if I missed it, please correct me).

Basically, my quest

相关标签:
13条回答
  • 2020-12-02 09:03

    From what I can tell, all of these answers want to fill the collection view from right to left. For example, if you labeled cells 1, 2, 3, then they would appear in the order 3, 2, 1 in the collection view. In my case I wanted the cells to appear in the order 1, 2, 3 (like text right alignment when the line is not full). In order to do this I created a simple UICollectionViewFlow layout.

    import UIKit
    
    class RightAlignFlowLayout: UICollectionViewFlowLayout {
    
        override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
        {
            guard let attrsArr = super.layoutAttributesForElements(in: rect) else {
                return nil
            }
            guard let collectionView = self.collectionView else { return attrsArr }
            if self.collectionViewContentSize.width > collectionView.bounds.width {
                return attrsArr
            }
    
            let remainingSpace = collectionView.bounds.width - self.collectionViewContentSize.width
    
            for attr in attrsArr {
                attr.frame.origin.x += remainingSpace
            }
    
            return attrsArr
        }
    
        override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
            guard let attrs = super.layoutAttributesForItem(at: indexPath) else { return nil }
            guard let collectionView = self.collectionView else { return attrs }
            if self.collectionViewContentSize.width > collectionView.bounds.width {
                return attrs
            }
    
            let remainingSpace = collectionView.bounds.width - self.collectionViewContentSize.width
            attrs.frame.origin.x += remainingSpace
            return attrs
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 09:04

    By changing both flipsHorizontallyInOppositeLayoutDirection and developmentLayoutDirection, I was able to achieve a full screen RTL scroll where the first item was all the way to the right, and to reach the last cell, user will need to scroll to the left.

    Like so:

    class RTLCollectionViewFlowLayout: UICollectionViewFlowLayout {
    
        override var flipsHorizontallyInOppositeLayoutDirection: Bool {
            return true
        }
    
        override var developmentLayoutDirection: UIUserInterfaceLayoutDirection {
            return UIUserInterfaceLayoutDirection.rightToLeft
        }
    }
    
    0 讨论(0)
  • 2020-12-02 09:09

    For anyone who has the same question:

    I ended up using the UICollectionViewRightAlignedLayout library that @chinttu-roxen-ramani recommended. You can either set it with code:

    self.collectionView.collectionViewLayout = [[UICollectionViewRightAlignedLayout alloc] init];
    

    or through interface builder:

    Through interface builder

    I ended up making a couple modifications to the library, but overall it works great.

    0 讨论(0)
  • 2020-12-02 09:15

    For anybody trying to achieve Right to Left layout of UICollectionView in Swift

    //in viewDidLoad
        YourCollectionView.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
    
    //in cellForItemAtIndexPath
        cell.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
    
    0 讨论(0)
  • 2020-12-02 09:20

    In addition to Tawfik's answer:

    You can also set UICollectionView's Semantic property via Interface Builder:

    More about this property: in this question

    0 讨论(0)
  • 2020-12-02 09:22

    You can get similar result by performing a transform on the collection view and reverse the flip on its content:

    First when creating the UICollectionView I performed a horizontal flip on it:

    [collectionView_ setTransform:CGAffineTransformMakeScale(-1, 1)];
    

    Then subclass UICollectionViewCell and in here do the same horizontal flip on its contentView:

    [self.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
    
    0 讨论(0)
提交回复
热议问题