iOS中UICollectionView自适应
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、关于cell,不要在layoutsubViews里适配,直接在init适配
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, 12, 10, 12));
}];