Last active
May 18, 2020 17:27
-
-
Save wdchris/4e1ff210252e17505edfebdb85299840 to your computer and use it in GitHub Desktop.
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
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