Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView scrollToItem Not Working in iOS 14

I'm using a collection view, UICollectionView, and it works absolutely great...except that I cannot scroll to any particular item. It seems to always scroll to the "middle" is my best guess. In any case, whatever I send to scrollToItem seems to have no effect on the scrolling. I've put it in various locations throughout my view controller but with no success.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let lastIndex = IndexPath(row: messages.count-1, section: 0)
    self.messagesView.scrollToItem(at: lastIndex, at: .bottom, animated: true)
}
like image 700
David J Avatar asked Nov 27 '22 23:11

David J


1 Answers

UICollection View has bug in iOS 14 with scrollToItem. In iOS 14 it will only work if collection view paging will be desable. So i got a solution if we have to scroll with both manual and programatically.

Specially for iOS 14 and above

  self.collView.isPagingEnabled = false
  self.collView.scrollToItem(at: IndexPath(item: scrollingIndex, section: 0), at: .left, animated: true)
  self.collView.isPagingEnabled = true
like image 144
Vikas Rajput Avatar answered Dec 24 '22 20:12

Vikas Rajput