如何获取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/