自定义layout中需要重写的方法

Layout类中,有3个方法是必定会被依次调用:

  1. prepareLayout: 准备所有view的layoutAttribute信息

  2. collectionViewContentSize: 计算contentsize,显然这一步得在prepareLayout之后进行

  3. layoutAttributesForElementsInRect: 返回在可见区域的view的layoutAttribute信息,

    这个方法不写 集合视图显示不出来,这个方法是将保存的每个item的信息告诉集合视图,进行显示。

此外,还有其他方法可能会被调用:

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { } 
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { } 
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)decorationViewKind atIndexPath:(NSIndexPath *)indexPath { } 
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { }
原文地址:https://www.cnblogs.com/dabaomo/p/5205417.html