Created
March 10, 2016 01:58
-
-
Save makthrow/067ec222b81d6a1e2419 to your computer and use it in GitHub Desktop.
set fixed number of cells per row in UICollectionViewController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func collectionView(collectionView: UICollectionView, | |
layout collectionViewLayout: UICollectionViewLayout, | |
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { | |
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout | |
let totalSpace = flowLayout.sectionInset.left | |
+ flowLayout.sectionInset.right | |
+ (flowLayout.minimumInteritemSpacing * CGFloat(numberOfItemsPerRow - 1)) | |
let size = Int((collectionView.bounds.width - totalSpace) / CGFloat(numberOfItemsPerRow)) | |
return CGSize(width: size, height: size) | |
} | |
/*This allows for any spacing between the cells. It assumes an Int member variable called numberOfItemsPerRow and also that all the cells are square and the same size. As noted in jhilgert00's answer we must also react to orientation changes, but now by using viewWillTransitionToSize as willRotateToInterfaceOrientation is depreciated. | |
shareedit | |
answered Mar 6 at 12:35 | |
sgib | |
13 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Take note of implementing
UICollectionViewDelegateFlowLayout