Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 251
Xyand Avatar asked Jul 03 '14 16:07

Xyand


People also ask

How to add section header in collectionView swift?

Connecting the Section Header to Data swift in an additional editor pane and Control-drag from the label in the header view over to the file and name the outlet titleLabel. It will add the following code: class FlickrPhotoHeaderView: UICollectionReusableView { @IBOutlet weak var titleLabel: UILabel! }

What is swift Uicollectionview?

An object that manages an ordered collection of data items and presents them using customizable layouts.


1 Answers

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.

like image 88
spassas Avatar answered Sep 21 '22 03:09

spassas