UICollectionView项目顺序不能从右到左语言反转

时间:2022-08-22 11:55:05

I noticed a big issue where in right to left languages, the cells order is not properly reversed, only the alignment is correct. But only for horizontal flow layout, and if the collection view contain different cell sizes! Yes, I know this sound insane. If all the cells are the same size, the ordering and alignment is good!

我注意到一个很大的问题,从右到左语言,单元格顺序没有正确反转,只有对齐是正确的。但仅适用于水平流布局,如果集合视图包含不同的单元格大小!是的,我知道这听起来很疯狂。如果所有单元格大小相同,则排序和对齐都很好!

Here is what I got so far with a sample app (isolated to make sure this is the actual issue, and not something else):

以下是我到目前为止使用示例应用程序(隔离以确保这是实际问题,而不是其他内容):

(First bar should always be drawn blue, then size increase with index.) UICollectionView项目顺序不能从右到左语言反转

(第一个柱应始终绘制为蓝色,然后根据索引增加尺寸。)

Is this an internal bug in UICollectionViewFlowLayout (on iOS 11)? Or is there something obvious that I am missing?

这是UICollectionViewFlowLayout(在iOS 11上)的内部错误吗?或者有什么明显的东西让我失踪?

Here is my test code (Nothing fancy + XIB with UICollectionView):

这是我的测试代码(Nothing fancy + XIB with UICollectionView):

public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 6
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "test", for: indexPath)
    cell.backgroundColor = (indexPath.item == 0) ? UIColor.blue : UIColor.red
    return cell
}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: (10 * indexPath.item) + 20, height: 170)
}

2 个解决方案

#1


6  

Automatic right-to-left support on dynamically-sized UICollectionViews is not a supported configuration. For this to work, you need to explicitly sign up for automatic layout mirroring as follows:

动态大小的UICollectionViews上的自动从右向左支持不是受支持的配置。为此,您需要明确注册自动布局镜像,如下所示:

  • Create a subclass of UICollectionViewFlowLayout
  • 创建UICollectionViewFlowLayout的子类

  • Override flipsHorizontallyInOppositeLayoutDirection, and return true in Swift or YES in Objective-C
  • 覆盖flipsHorizo​​ntallyInOppositeLayoutDirection,并在Swift中返回true或在Objective-C中返回YES

  • Set that as the layout of your collection view
  • 将其设置为集合视图的布局

This property is defined on UICollectionViewLayout (parent of Flow), so you can technically use this property on any custom layout you already have.

此属性在UICollectionViewLayout(Flow的父级)上定义,因此您可以在任何已有的自定义布局上使用此属性。

#2


1  

I believe that for this you will have to implement your own custom collectionViewLayout - although I understand that one would expect that it would automatically work just as the right-to-left on the rest of the components.

我相信为此你必须实现自己的自定义collectionViewLayout - 虽然我理解人们会期望它会自动地在其他组件上从右向左工作。

#1


6  

Automatic right-to-left support on dynamically-sized UICollectionViews is not a supported configuration. For this to work, you need to explicitly sign up for automatic layout mirroring as follows:

动态大小的UICollectionViews上的自动从右向左支持不是受支持的配置。为此,您需要明确注册自动布局镜像,如下所示:

  • Create a subclass of UICollectionViewFlowLayout
  • 创建UICollectionViewFlowLayout的子类

  • Override flipsHorizontallyInOppositeLayoutDirection, and return true in Swift or YES in Objective-C
  • 覆盖flipsHorizo​​ntallyInOppositeLayoutDirection,并在Swift中返回true或在Objective-C中返回YES

  • Set that as the layout of your collection view
  • 将其设置为集合视图的布局

This property is defined on UICollectionViewLayout (parent of Flow), so you can technically use this property on any custom layout you already have.

此属性在UICollectionViewLayout(Flow的父级)上定义,因此您可以在任何已有的自定义布局上使用此属性。

#2


1  

I believe that for this you will have to implement your own custom collectionViewLayout - although I understand that one would expect that it would automatically work just as the right-to-left on the rest of the components.

我相信为此你必须实现自己的自定义collectionViewLayout - 虽然我理解人们会期望它会自动地在其他组件上从右向左工作。