I'm trying to add UIView
s between my UICollectionViewCell
s in my UICollectionView
and I don't know how I could do that. I'm trying to accomplish something like this:
I'll probably need to write a custom UICollectionViewLayout
, but I don't really know where to start.
If you're using sections and the layout is appropriate for it, you might use section headers and footers.
But based on your illustration, it looks like you just need to define the UICollectionViewCell to contain those views. So, where you register your class:
put those border images in the UICollectionView cell subclass (in the above case, "CollectionViewCell"). That seems like the easiest approach.
Here's one I use:
Looks like if your collectionView background was green and contentView white you could get the horizontals with a space between the cells minimumLineSpacing. The vertical gap would be the tricky part, but if you were creative with your contentView and set the minimumInteritemSpacing carefully you could get it.
I studied more of how
UICollectionViewLayout
s work and figured out how to solve it. I have anUICollectionReusableView
subclass calledOrangeView
that will be positioned between my views, than I wrote anUICollectionViewFlowLayout
subclass calledCategoriesLayout
that will deal with my layout.Sorry for the big block of code, but here is how it looks like:
I also wrote a category with some methods to check if an index path is the last in a line, in the last line or the last in a section:
And this is the final result:
Wow. That's a lot of code in the other answers just for a separator line between rows..
This is how I solved it. First you'll need to add the line separator inside the cell. Make sure you keep dragging it making it wider than the actual cell width so if your cell width is 60p your separator line will be 70.
inside
cellForItemAtIndexPath
:If you want a separator at the end of the collection view but there's a chance that you won't get 4 cells at the last row you can add a simple Collection Reusable View as Footer.