1、效果圖:

2、配置collection
- (UICollectionView *)collectionView {
? ? if (!_collectionView) {
? ? ? ? UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
? layout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize;// 自適應
? ? ? ? if([layoutrespondsToSelector:@selector(_setRowAlignmentsOptions:)]) {// cell的對齊方式
? ? ? ? ? ? [layoutperformSelector:@selector(_setRowAlignmentsOptions:)withObject:@{@"UIFlowLayoutCommonRowHorizontalAlignmentKey":@(NSTextAlignmentLeft)}];
? ? ? ? }
? ? ? ? _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
? ? }
? ? return _collectionView;
}
3、獲取collectionView的總高度
self.collectionView.collectionViewLayout.collectionViewContentSize.height;
4、關(guān)于cell,不要在layoutsubViews里適配,直接在init適配
? [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
? ? ? ? ? ? make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, 12, 10, 12));
? ? ? ? }];