I am using a UICollectionView in my project, where there are multiple cells of differing widths on a line. According to: https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html
it spreads the cells out across the line with equal padding. This happens as expected, except I want to left justify them, and hard code a padding width.
I figure I need to subclass UICollectionViewFlowLayout, however after reading some of the tutorials etc online I just don't seem to get how this works.
The simple solution in 2019
This is one of those depressing questions where things have changed a lot over the years. It is now easy.
Basically you just do this:
All you need now is the boilerplate code:
Simply copy and paste that in to a
UICollectionViewFlowLayout
- you're done.Full working solution to copy and paste:
This is the whole thing:
And finally...
Give thanks to @AlexShubin above who first clarified this!
If anyone of you facing issue - some of the cells that's on the right of the collection view exceeding the bounds of the collection view. Then please use this -
Use INT in place of comparing CGFloat values.
There are many great ideas included in the answers to this question. However, most of them have some drawbacks:
Most solutions only override the
layoutAttributesForElements(in rect: CGRect)
function. They do not overridelayoutAttributesForItem(at indexPath: IndexPath)
. This is a problem because the collection view periodically calls the latter function to retrieve the layout attributes for a particular index path. If you don't return the proper attributes from that function, you're likely to run into all sort of visual bugs, e.g. during insertion and deletion animations of cells or when using self-sizing cells by setting the collection view layout'sestimatedItemSize
. The Apple docs state:Many solutions also make assumptions about the
rect
parameter that is passed to thelayoutAttributesForElements(in rect: CGRect)
function. For example, many are based on the assumption that therect
always starts at the beginning of a new line which is not necessarily the case.So in other words:
Most of the solutions suggested on this page work for some specific applications, but they don't work as expected in every situation.
AlignedCollectionViewFlowLayout
In order to address these issues I've created a
UICollectionViewFlowLayout
subclass that follows a similar idea as suggested by matt and Chris Wagner in their answers to a similar question. It can either align the cells⬅︎ left:
or ➡︎ right:
and additionally offers options to vertically align the cells in their respective rows (in case they vary in height).
You can simply download it here:
https://github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout
The usage is straight-forward and explained in the README file. You basically create an instance of
AlignedCollectionViewFlowLayout
, specify the desired alignment and assign it to your collection view'scollectionViewLayout
property:(It's also available on Cocoapods.)
How it works (for left-aligned cells):
The concept here is to rely solely on the
layoutAttributesForItem(at indexPath: IndexPath)
function. In thelayoutAttributesForElements(in rect: CGRect)
we simply get the index paths of all cells within therect
and then call the first function for every index path to retrieve the correct frames:(The
copy()
function simply creates a deep copy of all layout attributes in the array. You may look into the source code for its implementation.)So now the only thing we have to do is to implement the
layoutAttributesForItem(at indexPath: IndexPath)
function properly. The super classUICollectionViewFlowLayout
already puts the correct number of cells in each line so we only have to shift them left within their respective row. The difficulty lies in computing the amount of space we need to shift each cell to the left.As we want to have a fixed spacing between the cells the core idea is to just assume that the previous cell (the cell left of the cell that is currently laid out) is already positioned properly. Then we only have to add the cell spacing to the
maxX
value of the previous cell's frame and that's theorigin.x
value for the current cell's frame.Now we only need to know when we've reached the beginning of a line, so that we don't align a cell next to a cell in the previous line. (This would not only result in an incorrect layout but it would also be extremely laggy.) So we need to have a recursion anchor. The approach I use for finding that recursion anchor is the following:
To find out if the cell at index i is in the same line as the cell with index i-1 ...
... I "draw" a rectangle around the current cell and stretch it over the width of the whole collection view. As the
UICollectionViewFlowLayout
centers all cells vertically every cell in the same line must intersect with this rectangle.Thus, I simply check if the cell with index i-1 intersects with this line rectangle created from the cell with index i.
If it does intersect, the cell with index i is not the left most cell in the line.
→ Get the previous cell's frame (with the index i−1) and move the current cell next to it.
If it does not intersect, the cell with index i is the left most cell in the line.
→ Move the cell to the left edge of the collection view (without changing its vertical position).
I won't post the actual implementation of the
layoutAttributesForItem(at indexPath: IndexPath)
function here because I think the most important part is to understand the idea and you can always check my implementation in the source code. (It's a little more complicated than explained here because I also allow.right
alignment and various vertical alignment options. However, it follows the same idea.)Wow, I guess this is the longest answer I've ever written on Stackoverflow. I hope this helps.
Here is the original answer in Swift. It still works great mostly.
Exception: Autosizing Cells
There is one big exception sadly. If you're using
UICollectionViewFlowLayout
'sestimatedItemSize
. InternallyUICollectionViewFlowLayout
is changing things up a bit. I haven't tracked it entirely down but its clear its calling other methods afterlayoutAttributesForElementsInRect
while self sizing cells. From my trial and error I found it seems to calllayoutAttributesForItemAtIndexPath
for each cell individually during autosizing more often. This updatedLeftAlignedFlowLayout
works great withestimatedItemSize
. It works with static sized cells as well, however the extra layout calls leads me to use the original answer anytime I don't need autosizing cells.Edited Angel García Olloqui's answer to respect
minimumInteritemSpacing
from delegate'scollectionView(_:layout:minimumInteritemSpacingForSectionAt:)
, if it implements it.I had the same problem, Give the Cocoapod UICollectionViewLeftAlignedLayout a try. Just include it in your project and initialize it like this: