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

--

--