简体   繁体   中英

How do I get rect for UICollectionView

I want to locate the frame of section header in UICollectionView. I have a similar situation for UITableView, and for that, I was able to get its rect by doing:

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

Is there a way to get the same result in UICollectionView? Thanks!

Sorry, guys. It was actually easy.

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

Swift version of @tsuyoski's answer :

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 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM