属性设置
- item数据不够一屏幕时要实现上下滚动
self.collectionView.alwaysBounceVertical = YES;
初始化
- 定义collectionView是需要有layout属性,并且cell需要注册
自动以headView的步骤
1、需要是UICollectionReusableView的子类
2、需要注册
3、如果需要自定高度,需要遵守UICollectionViewDelegateFlowLayout协议。实现代理方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
- 实现headerView的自动布局,如果设置布局label,需要把label的宽高都进行约束,才能实现自动布局效果
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
// headerView来自xib
CGSize size = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size;
}