Search code examples
iosswiftuicollectionviewuicollectionviewlayout

Adjust collectionView.scrollToItem to consider inset?


I'm sometimes scroll to the left of a cell like this:

collectionView.scrollToItem(
    at: IndexPath(row: 5, section: 0),
    at: .left, // TODO: Left ignores inset
    animated: true
)

This is how it starts out before scrollToItem implemented:

enter image description here

However, when I try to use scroll to item, it sticks the cell to the edge instead of considering the inset:

enter image description here

Is there an easy way to fix collectionView.scrollToItem to accommodate the insets?


Solution

  • You can use the regular method collectionView.scrollToItem if you set your inset in collectionView.contentInset instead of layout.sectionInset.