I have a scenario where I have a horizontal scrolling collection view has a number of X cells. The default width of the cell is 71 points.
Depending on the value of X, I might have less cells when their total width is less than the width of the collection view, which is fine. Bun if their total width (cell count * 71) is larger than the width of the collectionView, show a number of cells Y that fit perfectly and half of the next one, so the user knows is horizontally scrollable.
This is what I have now
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if dataSource.count * 71 <= collectionView.bounds.width {
return CGSize(width: 71, height: collectionView.bounds.height)
} else {
// if it's larger just show Y number of cells with the last one visible cut in half, so the user knows it's horizontally scrollable
}
}
}
Found my solution, thanks all for the help
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
let collectionViewWidth = self.width
let numberOfCellsToFit = round(collectionViewWidth / cellWidth)
let spaceBetweenCells = (collectionViewWidth - cellWidth/2) / (numberOfCellsToFit - 1)
return (spaceBetweenCells - cellWidth)
}