I am currently using UICollectionView
for the user interface grid, and it works fine. However, I'd like to be enable horizontal scrolling. The grid supports 8 items per page and when the total number of items are, say 4, this is how the items should be arranged with horizontal scroll direction enabled:
0 0 x x
0 0 x x
Here 0 -> Collection Item and x -> Empty Cells
Is there a way to make them center aligned like:
x 0 0 x
x 0 0 x
So that the content looks more clean?
Also the below arrangement might also be a solution I am expecting.
0 0 0 0
x x x x
I think you can achieve the single line look by implementing something like this:
You will have to play around with that number to figure out how to force the content into a single line. The first 0, is the top edge argument, you could adjust that one too, if you want to center the content vertically in the screen.
Another way of doing this is setting the
collectionView.center.x
, like so:In this case, only respecting the right inset which works for me.
Here is my solution with a few assumptions:
Feel free to adjust to meet your needs.
Centered layout with variable cell width:
Result:
In Swift, the following will distribute cells evenly by applying the correct amount of padding on the sides of each cell. Of course, you need to know/set your cell width first.
You can use https://github.com/keighl/KTCenterFlowLayout like this:
Not sure if this is new in Xcode 5 or not, but you can now open the Size Inspector through Interface Builder and set an inset there. That'll prevent you from having to write custom code to do this for you and should drastically increase the speed at which you find the proper offsets.