Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the rect of a UICollectionViewCell?

UITableView has the method rectForRowAtIndexPath:, but this does not exist in UICollectionView. I'm looking for a nice clean way to grab a cell's bounding rectangle, perhaps one that I could add as a category on UICollectionView.

like image 644
akaru Avatar asked Sep 20 '12 01:09

akaru


1 Answers

The best way I've found to do this is the following:

Objective-C

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

Swift

let attributes = collectionView.layoutAttributesForItem(at: indexPath) 

Then you can access the location through either attributes.frame or attributes.center

like image 92
simon Avatar answered Sep 19 '22 11:09

simon