How to get the rect of a UICollectionViewCell?

后端 未结 5 1815
北海茫月
北海茫月 2020-12-23 11:05

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

5条回答
  •  隐瞒了意图╮
    2020-12-23 11:44

    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)
    

提交回复
热议问题