自定义UICollectionViewlayout

版权声明:本文为博主原创文章,转载请标明文章来源。 https://blog.csdn.net/SunFlowerInRain/article/details/77164782

这里写图片描述

在iOS开发中,UICollectionView的作用主要的作用是用来展示格子样式的数据,相比于TableView,collectionView的布局更加的灵活,它可以通过自定义UICollectionViewLayout,来实现我们需要的UI效果。

UICollectionView的主要作用是用来展示格子样式的数据,相比于TableView,collectionView更加灵活,可以通过自定义UICollectionViewLayout,来控制CollectionView对象的布局,来实现我们需要的UI效果。

UICollectionViewLayout简述:

UICollectionViewLayout本身并不绘制UI,collectionView的UI绘制由代理data source来完成。

UICollectionViewLayout主要来负责控制每一个需要layout对象的position和size。控制的对象包括Cells,Supplementary views(header,footer),Decoration views(背景装饰)。实现自定义的layout需要重写父类UICollectionViewlayout的一系列方法。

需要被重写的方法:

collectionViewContentSize:设置width和height来设置collectionview的contentsize;

layoutAttributesForElementsInRect: 为每一个cell所在的rect设置layout attribute

layoutAttributesForItemAtIndexPath: 为每一个cell本身设置layout attribute.

layoutAttributesForSupplementaryViewOfKind:atIndexPath: (如果需要加载Supplementary views) 为supplementary views设置layout attribute.

layoutAttributesForDecorationViewOfKind:atIndexPath: (如果需要加载DecorationView) 为DecorationView设置layout attribute

shouldInvalidateLayoutForBoundsChange: 当layout的对象的bounds出现变化时是是否要重新进行layout布局。

项目地址:https://github.com/LINDreaming/DailyTools

猜你喜欢

转载自blog.csdn.net/SunFlowerInRain/article/details/77164782
今日推荐