How to get the rect of a UICollectionViewCell?

Only two lines of code is required to get perfect frame :

Objective-C

UICollectionViewLayoutAttributes * theAttributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath];

CGRect cellFrameInSuperview = [collectionView convertRect:theAttributes.frame toView:[collectionView superview]];

Swift 4.2

let theAttributes = collectionView.layoutAttributesForItem(at: indexPath)
let cellFrameInSuperview = collectionView.convert(theAttributes.frame, to: collectionView.superview)

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