David J
David J

Reputation: 1098

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)
}

Upvotes: 7

Views: 8967

Answers (5)

Mehrdad
Mehrdad

Reputation: 1208

When you are using CompositionalLayout do not set both

collectionView.isPagingEnabled = true

in setup of your collection and

let section = NSCollectionLayoutSection(group: group)
section.orthogonalScrollingBehavior = .paging

in UICollectionViewCompositionalLayout setup.

Only set the second one in UICollectionViewCompositionalLayout.

Upvotes: -1

Nicolai Harbo
Nicolai Harbo

Reputation: 1169

This is what worked for me, in case you want paging enabled:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    collectionView.isPagingEnabled = false
    collectionView.scrollToItem(
        at: IndexPath(item: 1, section: 0),
        at: .centeredVertically,
        animated: false
    )
    collectionView.isPagingEnabled = true
}

Upvotes: 0

MALIKK HABIB UR REHMAN
MALIKK HABIB UR REHMAN

Reputation: 880

Swift 5 / iOS 15

I was facing the same problem, so I tried this one-line code and get it done.

// here we slect the required cell instead of directly scrolling to the item and both work same. 

self.collectionView.selectItem(at: IndexPath(row: self.index, section: 0), animated: true, scrollPosition: .left)

Upvotes: 1

Vikas Rajput
Vikas Rajput

Reputation: 1874

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

Upvotes: 42

wottle
wottle

Reputation: 13619

You could try putting the scrolling code in viewDidLayoutSubviews which should get called after all table cells are loaded, which means your messages.count should work. Also, make sure you only have a single section in your collection view.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.scrollToBottom()
}

func scrollToBottom() {
    DispatchQueue.main.async {
        let lastIndex = IndexPath(item: messages.count-1, section: 0)
        self.messagesView.scrollToItem(at: lastIndex, at: .bottom, animated: true)
    }
}

Upvotes: 11

Related Questions