Last active
June 17, 2016 11:45
-
-
Save g0ld2k/83f2e7c39ff0bc6b3299 to your computer and use it in GitHub Desktop.
Centers Collection View Content
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
extension CommunityConnectViewController: UICollectionViewDelegateFlowLayout { | |
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { | |
// Translated from Objective-C version at: http://stackoverflow.com/a/27656363/309736 | |
let cellCount = CGFloat(viewModel.getNumOfItemsInSection(0)) | |
if cellCount > 0 { | |
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout | |
let cellWidth = flowLayout.itemSize.width + flowLayout.minimumInteritemSpacing | |
let totalCellWidth = cellWidth * cellCount | |
let contentWidth = collectionView.frame.size.width - collectionView.contentInset.left - collectionView.contentInset.right | |
if (totalCellWidth < contentWidth) { | |
let padding = (contentWidth - totalCellWidth) / 2.0 | |
return UIEdgeInsetsMake(0, padding, 0, padding) | |
} | |
} | |
return UIEdgeInsetsZero | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment