ios - 获取 UICollectionView 部分的框架

标签 ios uicollectionview

如何获取UICollectionView实例中整个section的frame(CGRect值)? 我知道 UITableView 有一个 rectForSection: 方法。我正在为 UICollectionView 寻找类似的东西。

我检查了 UICollectionViewFlowLayout 的文档,但找不到任何看起来像 rectForSection: 的内容。

最佳答案

给collectionView添加类别

  @implementation UICollectionView (BMRect)

- (CGRect)bm_rectForSection:(NSInteger)section {
    NSInteger sectionNum = [self.dataSource collectionView:self numberOfItemsInSection:section];
    if (sectionNum <= 0) {
        return CGRectZero;
    } else {
        CGRect firstRect = [self bm_rectForRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
        CGRect lastRect = [self bm_rectForRowAtIndexPath:[NSIndexPath indexPathForItem:sectionNum-1 inSection:section]];
        return CGRectMake(0, CGRectGetMinY(firstRect), CGRectGetWidth(self.frame), CGRectGetMaxY(lastRect) - CGRectGetMidY(firstRect));
    }
}

- (CGRect)bm_rectForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}

@end

关于ios - 获取 UICollectionView 部分的框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37616326/

相关文章:

ios - 从日志输出中识别并修复自动布局问题

ios - 在 UIView 中切透明孔

ios - 有没有办法知道 locationManager 是事件还是停止?

ios - 如果 Collection View 嵌入在 ios 中的 TableView 中,如何始终显示 Collection View 的第一项

xcode - 如何将 Collection View 数据传递到新的 View Controller

ios - UICollectionViewCell - 当单元格内容改变时动画单元格高度改变

ios - 来自 UICollectionView 的全屏图像查看器

ios - 如何重建开发 pod 的变化?

ios - 类型...的值没有成员

iphone - Cocos2D iPhone - 对象发送自动释放次数太多?