Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move to the selected cell in UiCollectionView

I'm a newbie in Swift, please bear with me.

I have created a UICollectionView that scrolls horizontally, my question is: how can you move to the selected icon programmatically?

I have already used didSelectedPath, whats next?

like image 393
Led Avatar asked Jan 24 '26 17:01

Led


1 Answers

You need to call scrollToItemAtIndexPath function:

Scrolls the collection view contents until the specified item is visible.

 func scrollToItemAtIndexPath(_ indexPath: NSIndexPath,
                atScrollPosition scrollPosition: UICollectionViewScrollPosition,
                        animated animated: Bool)

Parameters

indexPath

The index path of the item to scroll into view.

scrollPosition

An option that specifies where the item should be positioned when scrolling finishes. For a list of possible values, see UICollectionViewScrollPosition.

animated

Specify true to animate the scrolling behavior or false to adjust the scroll view’s visible content immediately.

Check UICollectionView class reference with more info.

like image 88
Evgeny Karkan Avatar answered Jan 26 '26 07:01

Evgeny Karkan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!