3

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!

Kampai
  • 22,848
  • 21
  • 95
  • 95
tsuyoski
  • 614
  • 5
  • 22

2 Answers2

5

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];
tsuyoski
  • 614
  • 5
  • 22
0

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 }
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256