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

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> …

Read more

Subview frame is incorrect when creating UICollectionViewCell

You can get the final frames of your cell by overriding layoutIfNeeded() in your custom Cell class like this: override func layoutIfNeeded() { super.layoutIfNeeded() self.subView.layer.cornerRadius = self.subView.bounds.width / 2 } then in your UICollectionView data Source method cellForRowAtIndexPath: do this: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(“Cell”, forIndexPath: indexPath) as! CustomCollectionViewCell cell.setNeedsLayout() cell.layoutIfNeeded()

Resize UICollectionView cells after their data has been set

I think your are looking for the invalidateLayout method you can call on the .collectionViewLayout property of your UICollectionView. This method regenerates your layout, which in your case means also calling -collectionView: layout: sizeForItemAtIndexPath:, which is the right place to reflect your desired item size. Jirune points the right direction on how to calculate them. …

Read more

How to create UICollectionViewCell programmatically

Try to copy and paste this code into your xcode, it should work // // HomeVIewController.swift // Photolancer // // Created by Lee SangJoon on 9/8/16. // Copyright © 2016 Givnite. All rights reserved. // import UIKit class HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { var collectionview: UICollectionView! var cellId = “Cell” override func viewDidLoad() { super.viewDidLoad() …

Read more

Auto Layout in UICollectionViewCell not working

Well, I just looked on the iOS developer forums. Apparently, this is a bug with the iOS 8 SDK running on iOS 7 devices. The workaround is to add the following to your subclass of UICollectionViewCell: – (void)setBounds:(CGRect)bounds { [super setBounds:bounds]; self.contentView.frame = bounds; } override var bounds: CGRect { didSet { contentView.frame = bounds …

Read more

Dynamic size UICollectionView cell

Sorry this is super late… But maybe this will help people who haven’t found an answer yet. If you have your images in an array, you can simply reference the image size and make the adjustments to the cell size from there: – (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { UIImage *image = [imageArray objectAtIndex:indexPath.row]; //You …

Read more