When using collectionView.scrollToItem
to scroll to a given index path the focus is not set to the new cell.
Example:
let indexPath = IndexPath(item: item, section: section)
collectionView.scrollToItem(
at: indexPath,
at: .left,
animated: false
)
if let cell = collectionView.cellForItem(at: indexPath) {
assert(cell.isFocused) // false
}
Because the cell is not focused, if the user manually swipes to the another cell after the collectionView have to been programmatically scrolled, it’ll result in the collection view jumping two (or several) cells, as it’s moving relative to the cell it think is focused (e.g. the last user selected cell, not the programmatically selected one)
I’ve tried manually updating the focus on the cell:
cell.setNeedsFocusUpdate()
cell.updateFocusIfNeeded()
or the collection view
collectionView.setNeedsFocusUpdate()
collectionView.updateFocusIfNeeded()
To no avail.
Any idea how to work around this and make sure the programmatically scrolled cell have the attention of the focus engine?