Skip to content

Instantly share code, notes, and snippets.

@g0ld2k
Last active June 17, 2016 11:45
Show Gist options
  • Save g0ld2k/83f2e7c39ff0bc6b3299 to your computer and use it in GitHub Desktop.
Save g0ld2k/83f2e7c39ff0bc6b3299 to your computer and use it in GitHub Desktop.

Revisions

  1. g0ld2k renamed this gist Mar 3, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. g0ld2k created this gist Mar 3, 2016.
    23 changes: 23 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@

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