Getting the screen location of a cell from a UICollectionView Getting the screen location of a cell from a UICollectionView ios ios

Getting the screen location of a cell from a UICollectionView


-(void)collectionView:(UICollectionView *)cv didSelectItemAtIndexPath:(NSIndexPath *)indexPath;{UICollectionViewLayoutAttributes *attributes = [cv layoutAttributesForItemAtIndexPath:indexPath];CGRect cellRect = attributes.frame;CGRect cellFrameInSuperview = [cv convertRect:cellRect toView:[cv superview]];NSLog(@"%f",cellFrameInSuperview.origin.x);}

It work for me.You can try yourself


Well the first part of your question is pretty much clear, the second one?? anywayif what you want to get is the frame of the select cell in your collection you can use this :

UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath];CGRect cellRect = attributes.frame;

More info here


@Alivin solution using layoutAttributesForItemAtIndexPath works but only for the presented/current scroll view that the user sees.

Meaning, if you select the first presented visible cells you will get the actual frame, but if you scroll, the frame will have a deviation and you won't get the coordinates you need.

This is why you need to use convertPoint:toView :

let realCenter = collectionView.convertPoint(cell.center, toView: collectionView.superview)

Basically this method takes a point (cell.center) in one view and convert that point to another view (collectionView.superview) coordinate system which is exactly what we need.

Thus, realCenter will always contain the coordinates to the actual selected cell.