UICollectionView scrolls when a cell is selected

I am having problems when I try to select an element inside a UICollectionView, because when I click on it, it scrolls a bit.

I know that calledSelectItemAtIndexPath is called, but I want to prevent scrolling when selected. I just want the collection view to be viewed when the user scrolls, but if the user just clicks on a cell, he should not move. Only need to choose.

I hope you can help me, since I do not know how to prevent this problem.

Any help would be really appreciated.

+15
source share
5 answers

collectionview.selectItem(at: indexpath, animated: true, scrollPosition: .top) - , , ...

... , , , , , : collectionview.selectItem(at: indexpath, animated: true, scrollPosition: [])

+54

, , CollectionView , . , , CollectionView .

+5

:

collectionView.selectItem(at: newIdexPath, animated: true, scrollPosition: UICollectionViewScrollPosition(rawValue: 0))
+1

5

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)

}
0

Objective-C

[self.collectionView selectItemAtIndexPath:path animated:NO scrollPosition:UICollectionViewScrollPositionNone] 

Swift

collectionview.selectItemAtIndexPath(indexPath: path, animated: false, scrollPosition: None)
-3

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


All Articles