我们要使用CollectionView里面的头视图需要先注册头视图 UICollectionReusableView或者 继承UICollectionReusableView的子类,kind类型为UICollectionElementKindSectionHeader,并且需要带一个标识符,我们定义个static 的静态字符串就行,如下所示:
[collectionView registerClass:[UICollectionReusableViewclass ] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
几个重要的不能忽视的点就是 UICollectionViewFlowLayout 布局属性需要设置 headerReferenceSize头部的大小,不然头视图没有大小不显示;一定要在
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath; 方法里面创建头视图view并给出frame,然后添加到 UICollectionReusableView 中。
详细代码如下
- <pre name="code" class="objc">
-
- #import "HomeViewController.h"
- #import "ConstomCell.h"
-
-
- static NSString *headerViewIdentifier = @"hederview";
-
- @interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
-
- @property (nonatomic,strong) UIImageView *headerImage;
-
- @end
-
- @implementation HomeViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [self addCollectionView];
-
- }
-
- -(void)addCollectionView
- {
- UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
- layout.minimumLineSpacing=20;
- layout.itemSize=CGSizeMake(100, 100);
- layout.sectionInset=UIEdgeInsetsMake(0, 0, 50, 0);
- layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 250);
-
- UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
- collectionView.frame=self.view.bounds;
-
- [collectionView registerNib:[UINib nibWithNibName:@"ConstomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
-
- [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
-
- collectionView.backgroundColor=[UIColor whiteColor];
- collectionView.delegate=self;
- collectionView.dataSource=self;
- [self.view addSubview:collectionView];
- }
-
- #pragma mark 返回多少行
- -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
- {
-
- return 13;
- }
- #pragma markk 返回的单元格
- -(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
- {
- ConstomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
-
- return cell;
- }
-
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
- {
-
- if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
- UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath];
-
- [self addContent];
-
- [header addSubview:self.headerImage];
- return header;
- }
-
- return nil;
- }
- -(void)addContent
- {
- UIImageView *headerImage=[[UIImageView alloc]init];
- headerImage.contentMode=UIViewContentModeScaleAspectFill;
- headerImage.clipsToBounds=YES;
- headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250);
- headerImage.image=[UIImage imageNamed:@"mei"];
- self.headerImage=headerImage;
- }
-
-
- @end
最终效果

collectionView怎么添加头视图
原文:http://www.cnblogs.com/mgla/p/5667519.html