How to determine height of UICollectionView with FlowLayout

前端 未结 13 2081
醉话见心
醉话见心 2020-11-28 18:01

I\'ve got an UICollectionView with an UICollectionViewFlowLayout, and i want to calculate its content size (for return in intrinsicContentSiz

13条回答
  •  一整个雨季
    2020-11-28 18:37

    In case you are using Auto layout, then you could create a subclass of UICollectionView

    If you use the below the code then you don't have to specify any height constraints for the collection view as it would vary based on the contents of the collection view.

    Given below is the implementation:

    @interface DynamicCollectionView : UICollectionView
    
    @end
    
    @implementation DynamicCollectionView
    
    - (void) layoutSubviews
    {
        [super layoutSubviews];
    
        if (!CGSizeEqualToSize(self.bounds.size, [self intrinsicContentSize]))
        {
            [self invalidateIntrinsicContentSize];
        }
    }
    
    - (CGSize)intrinsicContentSize
    {
        CGSize intrinsicContentSize = self.contentSize;
    
        return intrinsicContentSize;
    }
    
    @end
    

提交回复
热议问题