Как да получа rect за UICollectionView

Искам да намеря рамката на заглавката на раздел в UICollectionView. Имам подобна ситуация за UITableView и за това успях да получа неговия rect, като направих:

 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:

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