我希望能够定位UICollectionView中节头的框架。对于UITableView,我遇到了类似的情况,我可以通过以下方式获取其矩形:
CGRect rect = [self.tableView rectForHeaderInSection:section];
有没有一种方法可以在UICollectionView中获得相同的结果?谢谢!
我希望能够定位UICollectionView中节头的框架。对于UITableView,我遇到了类似的情况,我可以通过以下方式获取其矩形:
CGRect rect = [self.tableView rectForHeaderInSection:section];
抱歉,伙计们。其实这很容易。
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:section];
UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForSupplementaryElementOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
CGRect rect = [attributes frame];
@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 }