cell上添加UICollectionView,Masonry+动态行高

1,413 阅读1分钟

研究了好久了,找到一个实现的方式,如何在cell非常简洁的实现九宫格功能。

效果如下:

cell添加collectionView,本身没什么难度,如果有几个坑点,容易走弯路。

首先collectionView 本身没法动态获取高度,所以我们需要在设置数据源方法里边,去更新高度


 [**self**.collectionView reloadData];

Mylog(@"%f",**self**.flowLayout.collectionViewContentSize.height);

[**self**.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {

make.height.equalTo(**self**.flowLayout.collectionViewContentSize.height);

}];

如果我们直接用Masonry设置collectionView,此时是没法获取到self.flowLayout.collectionViewContentSize.height的高度,如果前行 layoutifneed 会出现Masonry布局⚠️,看着很头疼。

!最简单的解决方式,就是collectionView 初始化的时候给个frame,屏幕大小都行。

然后就ok了。