【发布时间】:2023-03-30 09:14:01
【问题描述】:
我有一个由 UIView 包装的集合视图,集合视图与包装视图具有相同的边界。并且集合视图数据源将被更改,因此包装视图边界也将被更改。我已经用collectionView.collectionViewLayout.collectionViewContentSize实现了包装视图intrinsicContentSize方法,并且集合视图布局是UICollectionViewFlowLayout的子类,UICollectionView的单元格也实现了方法intrinsicContentSize,但是包装视图没有用集合更新框架查看内容大小。 collectionViewContentSize 始终是 CGRectZero。
我试过了
[self.layout invalidateLayout];
[self.collectionView reloadData];
[self.collectionView layoutIfNeeded];
但没用,
我应该怎么做才能用collectionView.collectionViewLayout.collectionViewContentSize 更新包装视图框架。
下面有一些代码:
ZXCollectionViewWrapperView *wrapperView = [[ZXCollectionViewWrapperView alloc] init];
[self.view addSubview:wrapperView];
[wrapperView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
}];
self.wrapperView = wrapperView;
/// wrapper view size
- (CGSize)intrinsicContentSize {
return self.collectionView.collectionViewLayout.collectionViewContentSize;
}
ZXCollectionViewAlignedLayout *layout = [[ZXCollectionViewAlignedLayout alloc] init];
self.layout = layout;
ZXCollectionView *collectionView = [[ZXCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.backgroundColor = [UIColor lightGrayColor];
[self addSubview:collectionView];
[collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
self.collectionView = collectionView;
[self.collectionView zx_registerCellClass:[ZXCommendCell class]];
self.dataSource = dataSource;
[self.collectionView reloadData];
[self.collectionView layoutIfNeeded];
【问题讨论】:
标签: ios uicollectionview uicollectionviewlayout