UICollectionView显示HeaderView FooterView 不如UITableView那么容易,常用会有两种做法:

1.Xib或者Storyboard 在属性一栏中设置一下:



如图所示,

2.代码设计Section的header和Footer:

好多都在找UICollectionView是否有这么个属性,比如上图说到Accessories什么的,其实不然。大家首先要搞明白意见事情,header和footer是追加视图,属于layout中的所以要代码设置section要在UICollectionViewFlowLayout:

- (UICollectionViewFlowLayout *) flowLayout{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.。。。。。。//各属性设置
flowLayout.headerReferenceSize = CGSizeMake(320.0f, 50.0f); //设置headerView大小
flowLayout.footerReferenceSize = CGSizeMake(320.0f, 50.0f); // 设置footerView大小
return flowLayout;
}
如果你用的是Xib或者Storyboard,不想在上图属性设置,想用代码:
- (void)viewDidLoad
{
[super viewDidLoad]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.headerReferenceSize = CGSizeMake(320.0f, 50.0f); //设置headerView大小
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"]; // 一定要设置
[self.collectionView setCollectionViewLayout:layout];
//(这部分说明可以参见xib设置sectionview)
} - (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WINSIZE.width, WINSIZE.width*7/15)];
[imageView sd_setImageWithURL:[NSURL URLWithString:[UMOnlineConfig getConfigParams:@"GuizeImageUrl"]] placeholderImage:kDefaultImage192_124];
[headerView addSubview:imageView];
return headerView;
}

运行结果:

UICollectionView添加 HeaderView FooterView的更多相关文章

  1. Android 5.X新特性之为RecyclerView添加HeaderView和FooterView

    上一节我们讲到了 Android 5.X新特性之RecyclerView基本解析及无限复用 相信大家也应该熟悉了RecyclerView的基本使用,这一节我们来学习下,为RecyclerView添加H ...

  2. UITableView HeaderView,FooterView 使用SnapKit布局导致约束异常

    今天做一个APP里面设置页面(个人中心) 就是一个列表菜单 顶部是一个头像和账户标题, 底部为一个退出登录按钮 当然我第一时间就想到了UITableView, HeaderView, FooterVi ...

  3. [Android]RecyclerView添加HeaderView出现宽度问题

    通过getItemViewType方式判断HeaderView方式添加HeaderView的,结果发现有几个界面HeaderView宽度不能满屏. 于是对比了几种布局,发现LinearLayout为根 ...

  4. ListView 添加 HeaderView常见错误

    1.addHeaderView异常: 最近在做通讯录开发时使用ListView,发现一个奇怪的问题:当添加一个ImageView 作为HeaderView时,发现ImageView长宽始终是1:1的大 ...

  5. Pull to RefreshListView 添加HeaderView

    使用listView.addHeaderView(view) 可以在 listView 上方添加一个view视图 ,使listView和这个view连接在一起 效果上看上去是一个整体 一般用于上拉刷新 ...

  6. iOS中UICollectionView添加头视图

    参考链接:https://www.jianshu.com/p/ef57199bf34a 找了一堆的博客,写的都少了很重要的一步. //引入头部视图 -(UICollectionReusableView ...

  7. Android RecycleView添加HeaderView宽度不能撑满屏幕问题

    下午做项目的时候,碰到页面布局需要使用RecycleView加HeaderView,本以为很简单东西,却有一个小小的坑:HeaderView布局的宽度不能撑满屏幕! 先看下出现问题的图: 天了噜,我写 ...

  8. Swift日常开发随笔

    1.修改UISearchBar的搜索框底色 使用以下代码: setSearchFieldBackgroundImage(CommonUseClass._sharedManager.imageFromC ...

  9. 2019最新Android常用开源库总结(附带github链接)

    前言 收集了一些比较常见的开源库,特此记录(已收录350+).另外,本文将持续更新,大家有关于Android 优秀的开源库,也可以在下面留言. 一 .基本控件 1.TextView HTextView ...

随机推荐

  1. php-fpm重启

    Ubuntu 18.04服务器 修改php init 文件后(/etc/php/7.2/fpm/php.ini)需要重启php-fpm,方法是: kill -USR2 `cat /run/php/ph ...

  2. 使用Spring Cache缓存出现的小失误

    前文:今天在使用Spring Boot项目使用Cache中出现的小失误,那先将自己创建项目的过程摆出来 1.首先创建一个Spring Boot的项目(我这里使用的开发工具是Intellij IDEA) ...

  3. double team

    队长博客链接 https://www.cnblogs.com/98-10-22-25/p/9806296.html 团队队名 泡面 团队成员 211606361 何承华(队长) 211606356 陈 ...

  4. Linux初次修改环境变量

    对于刚刚从windows系统跳到Linux的小白们,相信多少都会出现一些强迫症,希望能将Linux的Terminal能调出点色彩,让其好看些.之前自己也不知道怎么按着百度的教程操作完成的,直到组里新来 ...

  5. g++编译X265

    自己参考用: msys2 编译X265的命令 编译用工具安装 pacman -S git make tar automake autoconf libtool pkg-config mingw-w64 ...

  6. 4-3 重构发送post请求

    当创建好一个post请求后 怎么测试呢?

  7. IDEA高效运用技巧

    windows: //快捷鍵 1.项目之间的切换快捷键:Ctrl+Alt+[]. 2.文件之间切换快捷键:Ctrl+Alt+左右箭头. 3.返回到上一次修改的地方:Ctrl+Q. 4.查找打开过的文件 ...

  8. mysql 报错 Packets larger than max_allowed_packet are not allowed

    登录 mysql, 执行命令 : show variables like '%max_allowed_packet%'  重新设置: set global max_allowed_packet = 1 ...

  9. AngularJS入门-demo

    双向绑定测试: <body ng-app> 请输入姓名:<input ng-model="myname"> <br> {{myname}},你好 ...

  10. AX_ClassTemplate

    static void main(Args args) { THK_InterfaceDataExtract THK_InterfaceDataExtract; FormRun formRun = a ...