UICollectionView spacing is wrong for every other row

I am creating a custom flow layout for a UICollectionView and I am trying to get the spacing to the right. Despite setting minimumLineSpacing = 2

every other line, there is only 1px between each line of images, as can be seen here . I believe this is because Apple is now using dots instead of pixels, but how do you fix that? Here is my code in my CollectionViewController that sets the layout of the stream:

    let screenWidth = self.collectionView?.bounds.size.width
    let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    flowLayout.minimumInteritemSpacing = 2
    flowLayout.minimumLineSpacing = 2

    let totalSpacing = flowLayout.minimumInteritemSpacing * 4.0
    let imageSize = (screenWidth!-totalSpacing)/4.0

    flowLayout.itemSize = CGSize(width: imageSize, height: imageSize)

      

+3


source to share


1 answer


I understood that. If the cell height is a repeating float, the spacing between each row will alternate, every other row. For example, if I want to place three cells with 2 point spacing between each one, each cell will be 123.6666666667 points on iPhone 6. (iPhone 6 is 375 points, which results in the calculation (375-4) / 3 = 123, 6666666667). If I start using the same value for the height as for the width, the last 2/3 points will cause the rows to alternate as shown here .



I found that the cell height doesn't have to be an int to fix this; setting it to say 123.5 will fix the problem, but of course it will work too.

+1


source







All Articles