0

I am making a collection view which is horizontally scrolled. The collection view is in a uitableview cell
Collection View Image

My height of collection view is 170
I am making a cell of 75x75

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
return UIEdgeInsetsMake(4, 5, 6, 3);
}

This is my inset what I setting. Padding from top is 4 left is 5 right is 3 and bottom is 6. The collection view has 20 items horizontally scrolled. Now my problem is the vertical spacing of items when two rows are made. I want to keep it to minimum. I used this

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 2.5;
}

It provided a 2.5 padding between various items but still my vertical padding is too much as you can see from the image that vertical space between 2 lines of images is around 10. I just want to decrease that to 2.5. I did some change in return UIEdgeInset but if i increase padding from bottom, then the collection view only makes a single row that i dont want. for example if i use this

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(4, 5, 7, 3);
}

I get this output
Wrong Output

How to overcome this problem. I just want equal and minimum spacing between all my items. which are a square (75x75). Any help will be appreciated. Thanks

Community
  • 1
  • 1
Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98

2 Answers2

0

Use the following code to set the height of the collection view cell.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{

             var collectionViewHeight = collectionView.fram.size.height

                let cellheight = collectionViewHeight/2 - 5
                return CGSize(width: cell height , height: cellheight)

        }

now what it will do is that it will calculate the height of the collection view and divide it by 2 if you have fix 2 number of rows and and subtract 5 from each as collectionViewheight is 170 and divide by 2 is 85 and then - 5 is 80 height for every cell and with width equal for square cell

this will perfectly fit the view else if you can change the values according to you requirement

Fatti Khan
  • 1,543
  • 1
  • 11
  • 19
  • well it doesn't do what i want.. I want the vertical padding to be less – Rajan Maheshwari Mar 28 '15 at 10:53
  • oka wat you have to do is that every cell has been given a top and bottom margin so remove the bottom margin of the top cell means remove the bottom margin or every top cell or remove top margin of every bottom cell, when both the top of the lower cell and bottom of upper cell combines then they make it two time vertical space , do it by click collection view in storyboard and in right bar go to 5th tap and remove padding or do it programmatically – Fatti Khan Mar 28 '15 at 12:33
0

Minimum line spacing is just that -- it set the minimum spacing, but doesn't do anything to set the maximum. The collection view will add extra space between the rows if it needs to based on the size of the collection view. Your math doesn't add up. 2 X 75 (cell height) + 4 (top) + 6 (bottom) + 2.5 (interline spacing) = 162.5, but your collection view's height is 170. Change the collection view's height to 162.5, and it should work.

rdelmar
  • 103,982
  • 12
  • 207
  • 218