简体   繁体   中英

UICollectionview not scroll to item by selectItem function IOS

I can't scroll to the Item with this function of collectionView

collectionView.selectItem(at: IndexPath(row: 10 , section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally) 

This CollectionView is Horizontal

You are currently just selecting collection view item at indexPath, this will not scroll to item. You need to scroll at position of selected item . Add following method to scroll:

collectionView.selectItem(at: IndexPath(row: 10 , section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
collectionView.scrollToItem(at: IndexPath(row: 10 , section: 0), at: .centeredHorizontally, animated: true)

Thank you, everyone. I solved with this code. my mistake code is self.categoryItemsCollection.selectItem(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally) just change the scrollPosition parametr to .centeredHorizontally self.categoryItemsCollection.selectItem(at: IndexPath(row: indexPath.section, section: 0), animated: true, scrollPosition: .centeredHorizontally)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM