Select the Collection View in Photorama Scene in Main.storyBoard canvas, at Size Inspector reset all values in “Min Spacing” and “Section Insets” in Collection View section to zero.
Add the code below at the end of PhotosViewController.swift
Thanks for sharing that solution. Before doing this challenge I checked other tutorials and here is the solution, which works without any reloading. Place this line in PhotoViewController: let sectionInsets = UIEdgeInsets(top: 2.0, left: 2.0, bottom: 2.0, right: 2.0)
Those are the data from the size inspector we filled out manually. Then create the extension:
extension PhotosViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let itemsPerRow: CGFloat = 4
// We calculate paddingSpace. This is possible, because space is the same.
// We select one of the spaces, right or left
let paddingSpace = sectionInsets.left * (itemsPerRow + 1)
let availableWidth = view.frame.width - paddingSpace
let widthPerItem = availableWidth / itemsPerRow
return CGSize(width: widthPerItem, height: widthPerItem)
}
// This will reset the sectioninsets.
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return sectionInsets
}
}
One thing is missing - the spacing. We’ve got 2 points between the items and also as section insets. Between 4 items there are 3 spacings + 2 edge insets summing 5 in total and 5 multiplied by 2 is 10 points for all the spacings: