iOS UICollectionView 添加header fo

2018-03-02  本文已影响20人  illaclv
  1. 注册头部和底部视图
  [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeViewCollectionViewHeader"];
  [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"HomeViewCollectionViewFooter"];

2.头部视图和底部视图的大小

 UICollectionViewFlowLayout *_customLayout = [[UICollectionViewFlowLayout alloc] init]; // 自定义的布局对象
    _customLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
 //设置headerView大小
  _customLayout.headerReferenceSize = CGSizeMake(KScreenWidth, KWidth(560)); 
//设置footerView大小
 _customLayout.footerReferenceSize = CGSizeMake(KScreenWidth, KWidth(560));  
  1. 设置内容
#pragma mark - 视图内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    // 视图添加到 UICollectionReusableView 创建的对象中
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:HomeViewCollectionViewHeader forIndexPath:indexPath];
        return headerView;

    }else if (kind == UICollectionElementKindSectionFooter) {
        // 底部试图
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:HomeViewCollectionViewFooter forIndexPath:indexPath];
        headerView.backgroundColor = [UIColor redColor];
        return headerView;
        
    }else {
        return nil;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读