UICollectionView items order not reversed in right to left languages

心不动则不痛 提交于 2019-12-06 05:29:23

问题


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.)

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

Here is my test code (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)
}

回答1:


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:

  • Create a subclass of UICollectionViewFlowLayout
  • Override flipsHorizontallyInOppositeLayoutDirection, and return true in Swift or YES in Objective-C
  • 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.




回答2:


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.



来源:https://stackoverflow.com/questions/48455545/uicollectionview-items-order-not-reversed-in-right-to-left-languages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!