This is related to but distinct from To use Flow Layout, or to Customize?.
Here is an illustration of what I’m trying to do:
I’m wondering if I can do this with a UICollectionViewFlowLayout
, a subclass thereof, or if I need to create a completely custom layout? Based on the WWDC 2012 videos on UICollectionView, it appears that if you use Flow Layout with vertical scrolling, your layout lines are horizontal, and if you scroll horizontally, your layout lines are vertical. I want horizontal layout lines in a horizontally-scrolling collection view.
I also don’t have any inherent sections in my model - this is just a single set of items. I could group them into sections, but the collection view is resizable, so the number of items that can fit on a page would change sometimes, and it seems like the choice of which page each item goes on is better left to the layout than to the model if I don’t have any meaningful sections.
So, can I do this with Flow Layout, or do I need to create a custom layout?
Can simply change Scroll Direction in UICollectionView.xib to Horizontal. And use with the correct order of elements in the array.
Here I share my simple implementation!
The .h file:
The .m file:
And finally, if you want a paginated behaviour, you just need to configure your UICollectionView:
Hoping to be useful enough.
The previous above implementation was not complete, buggy, and with fixed cell size. Here's a more literal translation for the code:
You're right – that's not how a stock horizontally-scrolling collection view lays out cells. I'm afraid that you're going to have to implement your own custom
UICollectionViewLayout
subclass. Either that, or separate your models into sections.Swift 4
Code:
This is the Swift 3 version of @GuilhermeSprint answer
Code:
Usage:
Result
My Delegates extension if you wanna check it also