Steven Breuls Steven Breuls - 6 months ago 144
Swift Question

iOS: `UICollectionview`, how to get a cell's `contentoffset` by it's `indexPath`

I'm using a

UICollectionview
with a circular layout. I'm trying to calculate the
contentOffset
per item but with the circular layout the full
contentsize
does not seem match the content.


  • The collectionview's total contentSize = 780

  • The content offset of the last item = 397(?)

  • The content offset per item = 33(?)



Is there any way I can get the offset for an item by it's
indexPath
or at least the correct (397) value for the last item's in the
collectionview
? I got these values by testing and printing the
contentoffset
but I would like to calculate these numbers (33 and 397) by code without having to scroll.

So is there a way to calculate a cell's
contentoffset
(?) inside the
collectionview
by it's
indexPath
?
Thanks!

Answer

I found the problem, the contentsize of the scrollview is the size of the content + the size of the uicollectionview's frame. That was the reason the contentSize returned a larger size than the actual content was.

so: (contentSizeX - collectionviews frame X) / cells = contentOffset per item (780 - 383) / 12 => 33