首页 > 代码库 > iOS-UICollectionViewLayout方法介绍

iOS-UICollectionViewLayout方法介绍

注意:UICollectionView的自定义功能就是自己去实现UICollectionViewLayout的子类,然后重写相应的方法来实现Cell的布局

1.当布局首次被加载时会调用prepareLayout函数

-(void)prepareLayout;

2.自定义ContentSize该方法会返回CollectionView的大小,这个方法也是自定义布局中必须实现的方法

-(CGSize)collectionViewContentSize; 

3.该方法返回一个数组,该数组中存放的是为每个Cell绑定的UICollectionViewLayoutAttributes属性

-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;

4.该方法中去定制每个Cell的属性

-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

5.该方法就是根据indexPath来获取Cell所绑定的layoutAtrributes, 然后去更改UICollectionViewLayoutAttributes对象的一些属性并返回

-(UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;

6.该方法是为Header View或者FooterView来定制其对应的UICollectionViewLayoutAttributes

-(UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)elementKind atIndexPath:(NSIndexPath *)indexPath;

UICollectionViewLayoutAttributes常用的属性:

@property (nonatomic) CGRect frame;@property (nonatomic) CGPoint center;@property (nonatomic) CGSize size;@property (nonatomic) CATransform3D transform3D;@property (nonatomic) CGRect bounds NS_AVAILABLE_IOS(7_0);@property (nonatomic) CGAffineTransform transform NS_AVAILABLE_IOS(7_0);@property (nonatomic) CGFloat alpha;@property (nonatomic) NSInteger zIndex; // default is 0@property (nonatomic, getter=isHidden) BOOL hidden; // As an optimization, UICollectionView might not create a view for items whose hidden attribute is YES

 

iOS-UICollectionViewLayout方法介绍