如何以编程方式在UICollectionView中启用/禁用节标题? [英] How can I enable/disable section headers in UICollectionView programmatically?

查看:86
本文介绍了如何以编程方式在UICollectionView中启用/禁用节标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式在UICollectionView中启用/禁用节标题?

可以很容易地在Storyboard(复选框)中完成它,但是在代码中如何做呢?

解决方案

您可以使用UICollectionViewDelegateFlowLayoutcollectionView:layout:referenceSizeForHeaderInSection:方法并返回CGSizeMake(0,0)或相应地设置UICollectionViewFlowLayoutheaderReferenceSize. >

修改: headerReferenceSize实际上是情节提要板用来显示/隐藏标题的属性.我从情节提要文件中添加了相关行

带有部分复选框 :

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="50" height="50"/></collectionViewFlowLayout>

带部分复选框 关闭

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="0" height="0"/></collectionViewFlowLayout>

编辑#2:

来自官方文档 :

流布局中的每个节都可以具有自己的自定义页眉和页脚.要为视图配置页眉或页脚,必须将页眉或页脚的大小配置为非零.您可以通过实现适当的委托方法或将适当的值分配给headerReferenceSize和footerReferenceSize属性来实现.如果页眉或页脚的大小为0,则相应的视图不会添加到集合视图中.

How can I enable/disable section headers in UICollectionView programmatically?

It can be easily done easily done in Storyboard (checkbox), but how about doing it in code?

解决方案

You can either use the collectionView:layout:referenceSizeForHeaderInSection: method of the UICollectionViewDelegateFlowLayout and return CGSizeMake(0,0) or set accordingly the headerReferenceSize of UICollectionViewFlowLayout.

Edit: headerReferenceSize is actually the property that storyboard uses to show/hide the headers. I've added the relevant lines from the Storyboard file

With section checkbox on:

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="50" height="50"/></collectionViewFlowLayout>

With section checkbox off

 <collectionViewFlowLayout key="collectionViewLayout" minimumLineSpacing="10" minimumInteritemSpacing="10" id="xAt-Uo-bMl">
           <size key="headerReferenceSize" width="0" height="0"/></collectionViewFlowLayout>

Edit #2:

From the official docs:

Each section in a flow layout can have its own custom header and footer. To configure the header or footer for a view, you must configure the size of the header or footer to be non zero. You can do this by implementing the appropriate delegate methods or by assigning appropriate values to the headerReferenceSize and footerReferenceSize properties. If the header or footer size is 0, the corresponding view is not added to the collection view.

这篇关于如何以编程方式在UICollectionView中启用/禁用节标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆