Search code examples
iosautolayoutflowlayout

Extra padding in flow layout for each row


I have a collection view with a flow layout that holds a variable number of cells. All of the cells are the same size and the line spacing is constant. The width of the view is small enough that the cells form a single column.

The problem is that as the number of cells increases, extra space gets added between the last cell and the bottom edge. If there is only one cell, there is no space: the cell's edge is right against the bottom. For each cell that's added, the gap increases by about 20 pixels.

There is no footer for the view. I am at a loss as to why this extra space is being added.


Solution

  • This was a stupid bug. Elsewhere we were calculating the height of the view using minimumLineSpacing, but the actual line spacing was being set using collectionView:layout:minimumLineSpacingForSectionAtIndex: so the former property was incorrect.

    Someone had hacked around this by multiplying minimumLineSpacing by 2, which happened to give the correct value in some cases, but ended up with extra padding in others.