UICollectionView高级-自定义大小

UICollectionView的高度自适应

代码

- (void)creatAutoLayout{
[self.contentView mas_makeConstraints:^(MASConstraintMaker*make) {
	make.left.top.mas_equalTo(0);
	make.width.mas_equalTo(300);
	make.bottom.mas_equalTo(self.testLabel.mas_bottom).offset(12.0);
}];

[self.testImage mas_makeConstraints:^(MASConstraintMaker*make) {
	make.left.width.top.mas_equalTo(self.contentView);
	make.height.mas_equalTo(150);
}];

[self.testLabel mas_makeConstraints:^(MASConstraintMaker*make) {
	make.top.mas_equalTo(self.testImage.mas_bottom).offset(10.0);
	make.left.width.mas_equalTo(self.testImage);
}];
}


- (UICollectionViewLayoutAttributes*)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes*)layoutAttributes {
[self setNeedsLayout];
[self layoutIfNeeded];
CGSize size = [self.contentView systemLayoutSizeFittingSize: layoutAttributes.size];
CGRect cellFrame = layoutAttributes.frame;
cellFrame.size.height= size.height;
layoutAttributes.frame= cellFrame;
return layoutAttributes;

}

你可能感兴趣的:(IOS高级)