Led
Led

Reputation: 517

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?

Upvotes: 3

Views: 2898

Answers (1)

Evgeny Karkan
Evgeny Karkan

Reputation: 9612

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.

Upvotes: 6

Related Questions