I'm using a UICollectionView
with only one section. Here's some visualization:
[ x x x ]
[ x x x ]
[ x x ]
If the last row in the UICollectionView
does not have all 3 cells filled, I'd like to center those cells like so:
[ x x x ]
[ x x x ]
[ x x ]
I've tried to implement solutions from the following places:
However, the solutions shifts all the cells, and I'd like to only shift the LAST row if that last row doesn't contain X number of cells since I only have one section.
I know how to set insets, but I don't know how to achieve this with being only one section and trying to adjust the inset on the last row of cells.
I just started using UICollectionView
and not sure how to go around this issue?
The short answer is: you can't, using the provided UICollectionViewFlowLayout by Apple
The longer answer is: You're going to have to create a customer UICollectionViewLayout subclass and place the element yourself. Start by going through this tutorial. Then you should have a better understanding of the inner workings to be able to create your own custom layout.
Sounds like you need everything a flow layout offers with a little customizations. Subclassing flow layout and overriding
layoutAttributesForElements
should do the job:A quick and dirty implementation will basically ask FlowLayout to do the layout for a given rect, then figure out what items appear in the last row. If there are less than 3 items, then space them out evenly.
Note that if you plan on animating insertions/deletions, you should also override
layoutAttributesForCellWithIndexPath:
and ensure it returns consistent results.Apple has a guide on FlowLayout with a section dedicated to subclassing.
Sample Playground:
https://gist.github.com/AnuragMishra/0a694dfa9be1a5eab9fc7368b69812ad