我们都知道UITableview有一个tableHeaderFooterView,这样我们在布局页面的时候,如果顶部有轮播图,可以直接把轮播图设置为tableView的HeaderFooterView,用起来很好用,但是,当我们用上CollectionView的时候,发现collectionView并没有HeaderFooterView这个属性,可是此时我们页面的架构是以colletctionView来实现的,而且collectionViewHeaderView也是有用到,如果有这样的需求,这里提供2种解决方案把:

  • 第一种方案:腾出一组作为collectionView的HeaderView,内容下移一组,也就是把section = 0这一组变相设置为headerView,内容依次下移,优势在于不需要添加任何视图,仅在原有colectionView的基础上操作,但是呢,由于第一组被强行占用,所有后面数据源在赋值的时候有可能要小心数组的访问不要越界。

  • 第二种方案:这种方案也比较简单,跟第一组方案的优势在于,真的是给collectionView添加了一个头,collectionView访问期间,不需要考虑的其它任何因素。

    我的思路是利用collectionView的contentInset和scrollViewInset属性,让collectionView的滚动区域和内容区域向下偏移一定的距离,这样,顶部就腾出空间,这个空间可以用来填补我们想要设置的header(可能是轮播图),创建一个自定义view,设置view的frame,然后让view的空间完全填充上面利用偏移腾出的空间,就可以完美解决上述问题了

思路是这样的思路,具体部分代码实现如下:
```
#import "ViewController.h"
#import "TYCustomCell.h"
#import "TYHeaderFooterView.h"

define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewController

-(UICollectionView *)collectionView{

if (!_collectionView) {

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

layout.itemSize = CGSizeMake(100, 100);

layout.minimumInteritemSpacing = 10;

layout.minimumLineSpacing = 10;

layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);

_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

_collectionView.delegate = self;

_collectionView.dataSource = self;

_collectionView.backgroundColor = [UIColor whiteColor];

[_collectionView registerClass:[TYCustomCell class] forCellWithReuseIdentifier:@"TYCustomCell"];

[_collectionView registerClass:[TYHeaderFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

[_collectionView registerClass:[TYHeaderFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

//设置滚动范围偏移200

_collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(200, 0, 0, 0);

//设置内容范围偏移200

_collectionView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);

_collectionView.alwaysBounceVertical = YES;

}

return _collectionView;

}

  • (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self setupUI];

    }

-(void)setupUI{

//给collectionView添加子控件 这里是作为头部 记得设置y轴为负值
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, -200, SCREEN_WIDTH, 200)];
header.backgroundColor = [UIColor cyanColor];
[self.collectionView addSubview:header]; //添加内容到视图上
[self.view addSubview:self.collectionView];

}

pragma mark - CollectonViewDataSource Delegate

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return 9;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

TYCustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TYCustomCell" forIndexPath:indexPath];

cell.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];

return cell;

}

//设置头部

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { //header

TYHeaderFooterView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];

header.backgroundColor = [UIColor redColor];

return header;

}else if([kind isEqualToString:UICollectionElementKindSectionFooter]){ //footer

TYHeaderFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];

footer.backgroundColor = [UIColor blueColor];

return footer;

}

return [UICollectionReusableView new];

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{

return CGSizeMake(SCREEN_WIDTH, 44);

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{

return CGSizeMake(SCREEN_WIDTH, 44);

}

iOS collectionView添加类似tableView的tableHeaderView的更多相关文章

  1. iOS Category 添加属性实现原理 - 关联对象

    iOS Category 添加属性实现原理 - 关联对象 RunTime为Category动态关联对象 使用RunTime给系统的类添加属性,首先需要了解对象与属性的关系.对象一开始初始化的时候其属性 ...

  2. 为普通Object添加类似AttachedProperty的属性

    为普通Object添加类似AttachedProperty的属性   周银辉 我们知道,在WPF中对应一个DependencyObject,我们很容易通过AttachedProperty来为类型附加一 ...

  3. DKNightVersion 的实现 --- 如何为 iOS 应用添加夜间模式

    在很多重阅读或者需要在夜间观看的软件其实都会把夜间模式当做一个 App 所需要具备的特性. 而如何在不改变原有的架构, 甚至不改变原有的代码的基础上, 就能为应用优雅地添加夜间模式就成为一个在很多应用 ...

  4. iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见

    iOS中关于动态Tableview中的cell数据传输的多线程问题解决之拙见 (2015-12-05 12:48:20)[编辑][删除] 转载▼     首先我们先明确一下问题: 1.因为UI是在主线 ...

  5. Xamarin SQLite教程Xamarin.iOS项目添加引用

    Xamarin SQLite教程Xamarin.iOS项目添加引用 使用直接方式访问SQLite数据库,需要将System.Data和Mono.Data.SQlite库导入到创建的项目中.下面将分别讲 ...

  6. 添加类似navigationController自带的返回按钮

    添加类似navigationController自带的返回按钮,效果如下: 一.UINavigationcontroller自带的navigationBar 是无法添加左箭头的返回按钮的 在网上搜索了 ...

  7. iOS mac添加证书 不能修改“System Roots”钥匙串错误

    iOS mac添加证书 不能修改“System Roots”钥匙串错误 如图: 解决方式: 打开钥匙串---登录---,直接把证书拖过来 然后,查看--我的证书,里面,找到证书,即可

  8. 安卓&IOS 手机添加O365 邮箱账户

    手机添加O365 邮件账户 一.Android手机添加O365邮件账户 1. 找到手机上“电子邮件” 2. 打开设置 3. 点击添加账户 4. 选择“Exchange” 5. 输入O365的邮箱账户和 ...

  9. 如何给TableView、CollectionView添加动效

    // // ViewController.m // tableViewAnimation // // Created by 冯敏 on 2018/3/13. // Copyright © 2018年 ...

随机推荐

  1. Babel的配置和使用

    自从 Babel 由版本5升级到版本6后,在安装和使用方式上与之前大相径庭,于是写了这篇入坑须知,以免被新版本所坑. 坑一:本地安装和全局安装 全局安装只需: $ npm install --glob ...

  2. 3.1常用类(java学习笔记)包装类及日期类

    一.包装类 java是一门面向对象的语言,秉承一切皆对象的思想. 可java中有一些基本数据类型并不是对象,有时可能需要将它们变为对象. 这时就需要用到我们的包装类了. 基本数据类型 包装类 int ...

  3. iOS静态库打包

    当了三年多程序员,第一次写随便,说来有些惭愧.想成为一个优秀的程序员,分析,思考,总结并且做记录是必不可少的,今天先从简单的命令总结记起.好了,废话不多说了,Let's start our games ...

  4. 使用Jenkins搭建iOS开发的CI服务器

    目录 简介     下载并运行     Jenkins配置         安装git插件         E-mail设置     自动化构建         远程仓库设置         触发条件 ...

  5. etcd集群日常维护

    配置文件和启动参数说明 命令行 | 配置文件 | 说明 data-dir | ETCD_DATA_DIR | 指定节点的数据存储目录,包括节点ID,集群ID,集群初始化配置,Snapshot文件,若未 ...

  6. kubernetes 实用 api list

    https://192.168.20.128:6443/api/v1/pods 原文来自https://segmentfault.com/a/1190000002937665 收集整理一些可能较常用的 ...

  7. npm中package-lock.json的作用:npm install安装时使用

    简单理解: XYZ 的格式 对应为: 主版本号.次版本号.修订号,版本号递增规则如下: 主版本号:当你做了不兼容的 API 修改, 次版本号:当你做了向下兼容的功能性新增, 修订号:当你做了向下兼容的 ...

  8. nginx静态资源配置

    解决EE工程中静态文件显示问题 在工程中本地测试没有问题,发现使用nginx配置了路径的页面,会获取不到相应页面的静态文件问题 静态文件的路径类似为: http://localhost:8080/sa ...

  9. 2017.5.16 comparator和comparable的比较及使用

    参考来自: http://blog.csdn.net/lifuxiangcaohui/article/details/41543347 http://www.cnblogs.com/liuyuanyu ...

  10. 开源jabber(XMPP)架设内部即时通讯服务的解决方案

    Jabber 是著名的即时通讯服务服务器,它是一个自由开源软件,能让用户自己架即时通讯服务器,可以在Internet上应用,也可以在局域网中应用.    XMPP(可扩展消息处理现场协议)是基于可扩展 ...