Skip to content

Instantly share code, notes, and snippets.

@g0ld2k
Last active June 17, 2016 11:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g0ld2k/83f2e7c39ff0bc6b3299 to your computer and use it in GitHub Desktop.
Save g0ld2k/83f2e7c39ff0bc6b3299 to your computer and use it in GitHub Desktop.
Centers Collection View Content
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