Reputation: 11
I have a collection view, which is scrollable horizontally. It displays a list of elements horizontally. This is my collection view
I want to add an element in the middle of the collection view where my scrollbar is currently at. Let's say I drag the scrollbar all the way to the right, then the added element should be in the middle of the rightmost section. How do I determine the scrollbar position in the collection view?
Upvotes: 1
Views: 1539
Reputation: 19842
There is a property on UICollectionView
(or rather of its based class UIScrollView
named contentOffset
- it indicates of how much content is currently moved (scrolled).
Then you need to use this value and combine it with half of collectionView.frame.size.width
to get a position at which you need to place your new element.
Now once you have that, you need to use - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
and iterate over all elements to find a proper indexPath
you need to use for new elements.
NSIndexPath
is required for inserting new elements at the end. Once you have that, you need to add new element to your dataSource
and reload UICollectionView
.
Upvotes: 1