Horizontal collection view not working

1k views Asked by At

I have a horizontal UICollectionView, and I want it to have one item per column, all in one row. These items are of varying size. In order to do this, I used this UICollectionViewFlowLayout:

let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 50)
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = CGFloat.greatestFiniteMagnitude

Then I initialized my UICollectionView with that layout.

The item sizes are correct, but sometimes they get put in the same column, which is bad. According to this answer, my last line should fix the problem, but it does not. The last line simply makes the items extremely far apart from each other. I also tested using collectionView.height instead of CGFloat.greatestFiniteMagnitude, but I got the same bad result.

How can I fix this, and might this be a Swift 4 bug? –– this is in Swift 4

1

There are 1 answers

0
IHaveAQuestion On BEST ANSWER

This seemed to fix the problem. I am not sure why this worked but it did :)

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return view.frame.height
}