I have a UICollectionView with a flow layout and each cell is a square. How do I determine the spacing between each cells on each row? I can't seem to find the appropriate settings for this. I see there's a min spacing attributes on the nib file for a collection view, but I set this to 0 and the cells doesn't even stick.
Any other idea?
Here it is for NSCollectionViewFlowLayout
Clean Swift solution, from an history of evolution:
Usage: the spacing between items is determined by delegate's
collectionView (_:layout:minimumInteritemSpacingForSectionAt:)
.I put it on github, https://github.com/Coeur/UICollectionViewLeftAlignedLayout, where I actually added a feature of supporting both scroll directions (horizontal and vertical).
The "problem" with
UICollectionViewFlowLayout
is that it applies a justified align to the cells: The first cell in a row is left-aligned, the last cell in a row is right-aligned and all other cells in between are evenly distributed with an equal spacing that's greater than theminimumInteritemSpacing
.There are already many great answers to this post that solve this problem by subclassing
UICollectionViewFlowLayout
. As a result you get a layout that aligns the cells left. Another valid solution to distribute the cells with a constant spacing is to align the cells right.AlignedCollectionViewFlowLayout
I've created a
UICollectionViewFlowLayout
subclass as well that follows a similar idea as suggested by matt and Chris Wagner that can either align the cells⬅︎ left:
or ➡︎ right:
You can simply download it from here, add the layout file to your project and set
AlignedCollectionViewFlowLayout
as your collection view's layout class:https://github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout
How it works (for left-aligned cells):
The concept here is to check if the current cell with index i and the previous cell with the index i−1 occupy the same line.
→ Move the cell to the left edge of the collection view (without changing its vertical position).
→ Get the previous cell's frame (with the index i−1) and move the current cell next to it.
For right-aligned cells...
... you do the same vice-versa, i.e. you check the next cell with the index i+1 instead.
To get a maximum interitem spacing, subclass UICollectionViewFlowLayout and override
layoutAttributesForElementsInRect:
andlayoutAttributesForItemAtIndexPath:
.For example, a common problem is this: the rows of a collection view are right-and-left justified, except for the last line which is left-justified. Let's say we want all the lines to be left-justified, so that the space between them is, let's say, 10 points. Here's an easy way (in your UICollectionViewFlowLayout subclass):
The reason this is so easy is that we aren't really performing the heavy lifting of the layout; we are leveraging the layout work that UICollectionViewFlowLayout has already done for us. It has already decided how many items go in each line; we're just reading those lines and shoving the items together, if you see what I mean.
There are a few things to consider:
Try changing the minimum spacing in IB, but leave the cursor in that field. Notice that Xcode doesn't immediately mark the document as changed. When you click in a different field, though, Xcode does notice that the document is changed and marks it so in the file navigator. So, be sure to tab or click over to a different field after making a change.
Save your storyboard/xib file after making a change, and be sure to rebuild the app. It's not hard to miss that step, and then you're left scratching your head wondering why your changes didn't seem to have any effect.
UICollectionViewFlowLayout
has aminimumInteritemSpacing
property, which is what you're setting in IB. But the collection's delegate can also have a method to determine the inter-item spacing. That method trump's the layout's property, so if you implement it in your delegate your layout's property won't be used.Remember that the spacing there is a minimum spacing. The layout will use that number (whether it comes from the property or from the delegate method) as the smallest allowable space, but it may use a larger space if it has space leftover on the line. So if, for example, you set the minimum spacing to 0, you may still see a few pixels between items. If you want more control over exactly how the items are spaced you should probably use a different layout (possibly one of your own creation).