How to get the rect of a UICollectionViewCell?

后端 未结 5 1847
北海茫月
北海茫月 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:56

    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

提交回复
热议问题