4

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.) enter image description here

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)
}
Ludovic Landry
  • 11,606
  • 10
  • 48
  • 80

2 Answers2

15

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:

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

wakachamo
  • 1,723
  • 1
  • 12
  • 18
  • Thanks! This is such a weird behavior, do you have a link to the doc where this is specified? – Ludovic Landry Feb 15 '18 at 23:25
  • Thanks, you saved me! – EnasAZ Sep 05 '18 at 18:58
  • this works perfectly in ios 11, but for ios 10, the behavior still not ideal Basically, i have a footer cell, some content cells, and a placeholder cell i show when there are no results in ios 11, the placeholder and footer cells are correctly placed r-to-l by default, but the content cells show up incorrectly. override the flow layout fixes this in ios 10, the placholder and footer cells are **incorrectly placed** even with the override, going l-to-r, and adding the override just makes sure the content cells don't overlap the footer cell anybody else experienced this before? – Binya Koatz Sep 13 '18 at 23:49
0

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.

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90