如何获取UICollectionView的矩形框

3

我希望能够定位UICollectionView中节头的框架。对于UITableView,我遇到了类似的情况,我可以通过以下方式获取其矩形:

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

有没有一种方法可以在UICollectionView中获得相同的结果?谢谢!
2个回答

5

抱歉,伙计们。其实这很容易。

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

0

@tsuyoski的答案的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 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接