Как получить rect для UICollectionView

Я хочу найти рамку заголовка раздела в UICollectionView. У меня аналогичная ситуация для UITableView, и для этого я смог получить ее прямо, выполнив:

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

Есть ли способ получить тот же результат в UICollectionView? Спасибо!


person tsuyoski    schedule 19.11.2014    source источник


Ответы (2)


Извините ребята. На самом деле это было легко.

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

Быстрая версия @tsuyoski 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 }
person Lance Samaria    schedule 08.07.2021