collectionView自適應高度 有兩個點需要注意
1.設置layout的estimatedItemSize屬性
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
flowLayout.estimatedItemSize = CGSizeMake((SCREEN_WIDTH-24), autoSize(150));
2.在cell中重新並實現preferredLayoutAttributesFittingAttributes:方法【這個方法經常被遺漏,這樣算出來的寬高就會有問題】
-(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;
}