ios – UICollectionView with UICollectionViewCompositionalLayout scrollToItem does not work

ios – UICollectionView with UICollectionViewCompositionalLayout scrollToItem does not work


I’ve horizontal UICollectionView with UICollectionViewCompositionalLayout and orthogonalScrollingBehavior = .groupPagingCentered, created like this:

    non-public lazy var compositionalLayout = UICollectionViewCompositionalLayout(sectionProvider: { [weak self] (_: Int, _: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in

    guard let self else { return nil }
    let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(self.cellWidth), heightDimension: .absolute(Const.Structure.itemSize.peak))
    let merchandise = NSCollectionLayoutItem(layoutSize: itemSize)
    merchandise.contentInsets = NSDirectionalEdgeInsets(high: 0, main: 0, backside: 0, trailing: 0)

    let groupSize = NSCollectionLayoutSize(widthDimension: .absolute(self.cellWidth), heightDimension: .absolute(Const.Structure.itemSize.peak))
    let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
    // group.contentInsets = NSDirectionalEdgeInsets(high: 0, main: 0, backside: 0, trailing: 0)

    let part = NSCollectionLayoutSection(group: group)
    part.orthogonalScrollingBehavior = .groupPagingCentered
    part.visibleItemsInvalidationHandler = { [weak self] (_, _, _) in
        guard let self else { return }
        let heart = self.contentView.convert(self.collectionView.heart, to: self.collectionView)
        guard let indexPath = self.collectionView.indexPathForItem(at: heart) else { return }
        .... // Merchandise change handler
    }
    return part
})

I have to scroll for precise merchandise in my assortment view, so I’m making an attempt:

collectionView.scrollToItem(at: IndexPath(row: indexOfItem, part: 0), at: .centeredHorizontally, animated: false)

however it’s scrolling to incorrect merchandise.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.