【问题标题】:How do I get rect for UICollectionView如何获得 UICollectionView 的 rect
【发布时间】:2014-11-19 07:47:59
【问题描述】:

我想在 UICollectionView 中定位节标题的框架。我对 UITableView 也有类似的情况,为此,我能够通过以下方式获得它的 rect:

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

有没有办法在 UICollectionView 中获得相同的结果?谢谢!

【问题讨论】:

    标签: ios objective-c uicollectionviewlayout cgrect


    【解决方案1】:

    对不起,伙计们。其实很简单。

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

    【讨论】:

      【解决方案2】:

      @tsuyoski's answer 的 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 }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多