CollectionViewCell 高度自适应

  • layout 布局子控件
  • 自定义的cell中实现
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{

    UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
    CGRect rect = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0);
    [self.contentView layoutIfNeeded];
    //imageView 是cell中 最后面的控件
    CGFloat maxH = CGRectGetMaxY(self.imageView.frame);
    rect.size.height = maxH+10;
    attributes.frame = rect;

    return attributes;
}


复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享