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
This is how i obtained a collection view that was center aligned with a fixed Interitem spacing
Similar to other answers, this is a dynamic answer that should work for static sized cells. The one modification I made is that I put the padding on both sides. If I didn't do this, I experienced problems.
Objective-C
Swift
Also, if you are still are experiencing problems, make sure that you set both the
minimumInteritemSpacing
andminimumLineSpacing
to the same values since these values seem to be interrelated.Based on user3676011 answer, I can suggest more detailed one with small correction. This solution works great on Swift 2.0.
There was an issue in
where should be additional
-1
mentioned.It's easy to calculate insets dynamically, this code will always center your cells:
Here is how you can do it and it works fine
Swift 2.0 Works fine for me!
Where: screenWight: basically its my collection's width (full screen width) - I made constants: let screenWight:CGFloat = UIScreen.mainScreen().bounds.width because self.view.bounds shows every-time 600 - coz of SizeClasses elements - array of cells 50 - my manual cell width 10 - my distance between cells