美文网首页
关于自定义UICollectionViewLayout

关于自定义UICollectionViewLayout

作者: 最后还是个农 | 来源:发表于2022-10-07 18:19 被阅读0次

自定义UICollectionViewLayout可以通过重写一下几种方法实现:

// 准备布局,布局配置数据,布局前会调用这个方法,注意要调用 [super prepareLayout];
- (void)prepareLayout;
// 返回collectionView的可以滚动的区域大小
- (CGSize)collectionViewContentSize;
// 返回所有的布局信息
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;
// 返回indexPath对应的cell的布局信息
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

还有两个没有用到过,留着

// 返回indexPath处的header或footer的布局信息
-(UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
// 返回indexPath处的装饰的布局信息
-(UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;

目前用到了两种,一种是瀑布流,还有一种是多行横向整页滚动。效果如下:
Demo地址

瀑布流.gif
多行横向整页滚动.gif
参考此文章实现

相关文章

网友评论

      本文标题:关于自定义UICollectionViewLayout

      本文链接:https://www.haomeiwen.com/subject/edybartx.html