UICollectionView scrollToItem not working in .rightToLeft app

Viewed 5

I have an app that uses a UICollectionView (collectionView) and uses scrollToItem to move between different cells. It works fine when the language is .leftToRight. It doesn't work when the language is .rightToLeft.

In the particular case I have there are only two cells (there used to be more, but things were removed from the app). So it should be simple.

I can see that after I call scrollToItem in some cases cellForItemAt is being called with the new index only to immediately be called with the old index as part of the same refresh cycle. In some cases it is never called with the new index.

I played around with all sorts of ways of manipulating the rightToLeft behavior of a UICollectionView (including the collectionFlowLayout).

1 Answers

What worked for me in the end was this:

In viewDidLoad:

    if UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft
    {
        self.collectionView.semanticContentAttribute = .forceLeftToRight
    }

That just makes it behave leftToRight and then it's happy and does what is expected. Remember that because the transition is invoked programmatically and user paging is not enabled, the experience for the user is not confusing.

Related