I'm using an UICollectionView
on which I want to place seven cells side by side. The whole screen should be used for this. Currently, I'm using the width of the collection view and divide it by seven. Now I get an item width of 45.71429 on an iPhone 4. Between some cells there is an interim spacing. How can I handle this? I want to fill out the whole screen and all items should have the same size.
One option which comes to my mind is to round the value and use the remaining value as inset. But isn't there a better way?
Step 1: Implement UICollectionViewDelegateFlowLayout(if not done).
Step 2: Use delegate method of UICollectionViewDelegateFlowLayout.
Step 3: Go to XIB or StoryBoard where you have your CollectionView.
Step 4: In XIB or StoryBoard where you have your CollectionView, click on CollectionView.
Step 5: Go to InterfaceBuilder, then in second last tab (ie: Size Inspector) set Min Spacing
For Cells = 0
For Lines = 0
That it.
Try this!
Most importantly add
UICollectionViewDelegateFlowLayout
andUICollectionViewDelegate
to your controllerImplement below methods for
inset
andsizeForItem
for your cell size.For 0 spacing between cells, we need to specify it’s layout like this.
Thanks. Hope this helps!
As you already figured out it's impossible to divide a 320 point wide screen into 7 equal portions.
But if a cell is a half or even one point larger or smaller than another one nobody will notice. You can use a little bit of math to get integer pixel values (i.e. 0.5 points).
That's just an example, that I had ready because I am currently working on a calendar view. It has a 1 pixel spacing between each cell. For a 320 pt wide cell layout without spacing you could use something like
45.5+45.5+46+46+46+45.5+45.5
Try this out, declare these variables
and add these methods
Here num_items=7 as in your case you want number of cells in a row to be 7