UICollectionView scrollToItem Issue and Its Fix(Xcode, iOS 14 and Swift)

Akshay Somkuwar
1 min readMay 13, 2021

Apparently there is a new bug in UICollectionView that is causing scrollToItem to not work when paging is enabled. The work around is to disable paging before calling scrollToItem, then re-enabling it afterwards

Actually i was facing this issue in Xcode 12 and iOS 14, i have wasted lot of time in fixing this issue, the was not working fine in Xcode 12 but it was working fine on Xcode 11.x.
Even the Builds created using Xcode 12 has the bug but if i created a build using Xcode 11.x then it was working fine.
So the bug is with iOS 14 or the Xcode is still not figured out. But we have the fix.
So let’s talk about the fix.

//First you have to disable the paging if you have enabled it and made it true
collectionView.isPagingEnabled = false
//Then call the scollToItem method
collectionView.scrollToItem(
at: IndexPath(item: indexToScrollValue, section: 0),
at: .centeredHorizontally,
animated: true
)
//After that you have to enable the paging if you have initially enabled it
collectionView.isPagingEnabled = true
//Thats it now the UICollectionView will scroll to the desired indexPath.

Thank you for reading..🙏

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Akshay Somkuwar
Akshay Somkuwar

Written by Akshay Somkuwar

iOS | MacOS | React Native Developer..

Responses (1)

Write a response