Skip to content

Instantly share code, notes, and snippets.

@wdchris
Last active May 18, 2020 17:27
Show Gist options
  • Save wdchris/4e1ff210252e17505edfebdb85299840 to your computer and use it in GitHub Desktop.
Save wdchris/4e1ff210252e17505edfebdb85299840 to your computer and use it in GitHub Desktop.
var itemsPerRow: CGFloat = 3
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let flow = collectionViewLayout as? UICollectionViewFlowLayout else {
fatalError("only flow layout is supported")
}
let paddingSpace = flow.sectionInset.left + flow.sectionInset.right + (minimumInteritemSpacing * itemsPerRow)
let availableWidth = collectionView.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
guard let flow = collectionViewLayout as? UICollectionViewFlowLayout else {
fatalError("only flow layout is supported")
}
return flow.sectionInset
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment