UICollectionView scrollToItem not working

My horizontal single line UICollectionViewhas an insert and content size:

let space = collectionView.width

collectionView.contentInset = UIEdgeInsets(top: 0, left: space / 2, bottom: 0, right: space / 2)
collectionView.contentSize  = CGSize(width: CGFloat(items.count) * cellWidth, height: 45)

When a cell is pressed, I want to scroll it to the center of scrolling. I was hoping this would only work:

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
    collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}

However, that’s all, it’s scrolling the left left side of the left side to the center, regardless of which cell is pressed.

What can i do wrong?

+4
source share

Source: https://habr.com/ru/post/1659184/


All Articles