如何获取UICollectionView的rect

时间:2014-11-19 07:48:00

标签: ios objective-c uicollectionviewlayout cgrect

我想在UICollectionView中找到节头的框架。我对UITableView有类似的情况,为此,我可以通过执行以下操作来获取它的直接:

 CGRect rect = [self.tableView rectForHeaderInSection:section];

有没有办法在UICollectionView中获得相同的结果?谢谢!

2 个答案:

答案 0 :(得分:3)

对不起,伙计们。这实际上很容易。

 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:section];
 UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
 CGRect rect = [attributes frame];

答案 1 :(得分:0)

@tsuyoski's answer 的 Swift 版本:

let section = 0 // or whatever section you're interested in

let indexPath = IndexPath(item: 0, section: section)
let attributes = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionView.elementKindSectionHeader, at: indexPath)
guard let rect = attributes?.frame else { return }