在UICollectionView上从右向左对齐

reb*_*o95 53 objective-c right-to-left ios uicollectionview uicollectionviewlayout

这是一个非常直截了当的问题,但是我无法在SO上找到明确的答案(如果我错过了,请纠正我).

基本上,我的问题是:是否可以UICollectionView从右到左而不是从左到右对齐行内容?

在我的研究中,我看到了建议子类化的答案UICollectionViewFlowLayout,但我还没有找到一个为右对齐创建一个例子的例子.

我的目标是设置2个集合视图,如下所示:

例

任何帮助是极大的赞赏!

Taw*_*bid 87

没有对集合视图进行任何Xtransform,只需强制RTL:

YourCollectionView.semanticContentAttribute = UISemanticContentAttribute.forceRightToLeft
Run Code Online (Sandbox Code Playgroud)


LK_*_*K__ 84

您可以通过对集合视图执行转换并反转其内容的翻转来获得类似的结果:

首先,在创建UICollectionView时,我在其上执行了水平翻转:

[collectionView_ setTransform:CGAffineTransformMakeScale(-1, 1)];
Run Code Online (Sandbox Code Playgroud)

然后子类UICollectionViewCell并在这里对其contentView执行相同的水平翻转:

[self.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
Run Code Online (Sandbox Code Playgroud)

  • 如果您的单元格中有任何子视图,那么您也需要对其进行转换。对于例如[[self.lblTitle setTransform:CGAffineTransformMakeScale(-1,1)];` (2认同)

med*_*ick 28

除了Tawfik的回答:

您还可以Semantic通过Interface Builder 设置UICollectionView的属性:

故事板

更多关于这个属性:在这个问题


Saq*_*mer 15

对于任何试图UICollectionView在Swift中实现从右到左布局的人

//in viewDidLoad
    YourCollectionView.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)

//in cellForItemAtIndexPath
    cell.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
Run Code Online (Sandbox Code Playgroud)

  • 否决了这一点,赞成将“UISemanticContentAttribute”响应向上移动 (2认同)
  • 赞成,因为您不应该反对正确答案 (2认同)

Rob*_*hen 14

从iOS 9开始,集合视图根据此WWDC视频支持RTL .因此,不再需要创建RTL流布局(除非您已经使用自定义布局).

选择:编辑方案... > 选项 > 运行 > 应用程序语言 > 从右到左伪语言

在此输入图像描述

当您构建到模拟器时,文本将右对齐,并且您的集合视图将从右到左排序.

在此输入图像描述

但是有一个问题.何时contentOffset.x == 0,集合视图滚动位置位于Left边缘(错误)而不是Right边缘(正确).请参阅此堆栈文章了解详细信息

一个解决方法是简单地将First项目滚动到.Left(有一个问题 - .Left实际上是在右边前沿边缘):

override func viewDidAppear(animated: Bool) {
    if collectionView?.numberOfItemsInSection(0) > 0  {
        let indexPath = NSIndexPath(forItem: 0, inSection: 0)
        collectionView?.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的测试项目中,我的Collection View嵌套在Table View Cell中,因此我没有访问权限viewDidAppear().所以相反,我最终挂钩drawRect():

class CategoryRow : UITableViewCell {
    @IBOutlet weak var collectionView: UICollectionView!

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
        scrollToBeginning()
    }

    override func prepareForReuse() {
        scrollToBeginning()
    }

    func scrollToBeginning() {
        guard collectionView.numberOfItems(inSection: 0) > 0 else { return }
        let indexPath = IndexPath(item: 0, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
    }
}
Run Code Online (Sandbox Code Playgroud)

要查看此操作,请查看此git repo上RTL分支.有关上下文,请参阅此博客文章及其评论.

在此输入图像描述


小智 10

extension UICollectionViewFlowLayout {

    open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
        return true
    }

}
Run Code Online (Sandbox Code Playgroud)


coh*_*n72 9

通过改变 flipsHorizontallyInOppositeLayoutDirectiondevelopmentLayoutDirection我是能够实现全屏RTL滚动,其中第一项是一路的权利,以达到最后一个单元格,用户需要将滚动到左侧。

像这样:

class RTLCollectionViewFlowLayout: UICollectionViewFlowLayout {

    override var flipsHorizontallyInOppositeLayoutDirection: Bool {
        return true
    }

    override var developmentLayoutDirection: UIUserInterfaceLayoutDirection {
        return UIUserInterfaceLayoutDirection.rightToLeft
    }
}
Run Code Online (Sandbox Code Playgroud)


m.a*_*adi 8

自iOS 11起即可使用:

extension UICollectionViewFlowLayout {

    open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
        return true
    }

}
Run Code Online (Sandbox Code Playgroud)


Kri*_*ana 8

添加以下扩展对我有用

extension UICollectionViewFlowLayout {
    open override var flipsHorizontallyInOppositeLayoutDirection: Bool {
        return true  //RETURN true if collection view needs to enable RTL
    }
}
Run Code Online (Sandbox Code Playgroud)


reb*_*o95 7

对于有同样问题的人:

我结束了使用UICollectionViewRightAlignedLayout库是@ chinttu-Roxen中,拉马尼建议.您可以使用代码设置它:

self.collectionView.collectionViewLayout = [[UICollectionViewRightAlignedLayout alloc] init];
Run Code Online (Sandbox Code Playgroud)

或通过界面构建​​器:

通过界面构建​​器

我最终对库进行了一些修改,但总的来说它很有效.