You need to provide proper value from minimumIntteItemSpacingForSectionAt function. You can try the sample code given below. Hope it would help.
```
import UIKit
class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
var compressedWidth: CGFloat {
return collectionView.bounds.width / 2
}
var expandedWidth: CGFloat {
return compressedWidth * 0.5
}
var edgeInsets: UIEdgeInsets {
return UIEdgeInsets(top: 20, left: 20, bottom: 0, right: 20)
}
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let genericCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
genericCell.backgroundColor = .red
return genericCell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return (collectionView.bounds.width - compressedWidth - expandedWidth - edgeInsets.left - edgeInsets.right).rounded(.down)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 40
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if UIDevice.current.userInterfaceIdiom == .phone {
return CGSize(width: collectionView.bounds.width - edgeInsets.left - edgeInsets.right, height: 80)
}
let cellHeight: CGFloat = 100
let numberOfItemsInRow = 2
let rowNumber = indexPath.item / numberOfItemsInRow
let isEvenRow = rowNumber % 2 == 0
let isFirstItem = indexPath.item % numberOfItemsInRow == 0
var width: CGFloat = 0.0
if isEvenRow {
width = isFirstItem ? expandedWidth : compressedWidth
} else {
width = isFirstItem ? compressedWidth : expandedWidth
}
return CGSize(width: width, height: cellHeight)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return edgeInsets
}
}
```
**Just replace**
cellWidth = (minimumWidth * settings.portion) - 0.5
**WITH**
cellWidth = (minimumWidth * settings.portion)
**Specified `minimumInteritemSpacingForSectionAt` to 0.5 instead of 0**
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.5
}
**CVTVCell.swift**
Just reload your `UICollectionView` in `awakeFromNib` method after your data set into `currentContents`. Please refer below sample code.
override func awakeFromNib(){
super.awakeFromNib()
currentContents = collectionContents.allEndings[0]
self.cView.reloadData()
}
[Download code from here][1]
[1]: https://drive.google.com/open?id=1RrQZP9MCNFbQlaHy86gNG8ECDrD9Ki3R