14

I have a collection view, is working fine, and I have adjusted the separation for the X padding,

and it works fine, but for the Y padding between cells, doesn't seem to adjust to NO separation

This is my code for the layout

UICollectionViewFlowLayout *layoutItem=[[UICollectionViewFlowLayout alloc] init];
    layoutItem.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

enter image description here So how can I set the separation on top / bottom to 0px? Between cells?,

thanks!

manuelBetancurt
  • 13,050
  • 29
  • 103
  • 194

2 Answers2

46

you will see only top y padding at first time. And For showing bottom y padding you need more data that CollectionView frame height. When you scroll up collection view you will see bottom y padding.

I used collectionView like this

https://stackoverflow.com/a/17856406/1305001

When I set

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(60, 10, 50, 10);
}

The output will come as First time..

enter image description here

When you scrolled up collectionView you will see bottom padding..

enter image description here

Use this for verticle line spacing between cells

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

Will look like

enter image description here

Community
  • 1
  • 1
Warewolf
  • 12,416
  • 4
  • 55
  • 85
  • hi thanks, my mistake I didn’t explain properly, is the separation between cells on top, bottom, meaning, no space on y between cells, I edited my question and up-voted you thanks – manuelBetancurt Aug 03 '13 at 06:44
  • check edited answer,pass 0 instead of 5 will let you have no vertical space between cells. – Warewolf Aug 03 '13 at 07:08
5

You can also do it a much simpler way using the collection view flow layout.

Set up the flow layout (remember to add the delegate in your header):

UICollectionViewFlowLayout * stickerFlowLayout = [[UICollectionViewFlowLayout alloc] init];

stickerFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
**stickerFlowLayout.minimumLineSpacing = 10;**
stickerFlowLayout.minimumInteritemSpacing = 5;
stickerFlowLayout.sectionInset = UIEdgeInsetsMake(10, 25, 20, 25);

// Set up the collection view 
collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:stickerFlowLayout];
collectionView.delegate = self;
collectionView.dataSource = self;

etc

As you can see we can easily set the line spacing using:

**stickerFlowLayout.minimumLineSpacing = 10;**

We can also alter the other attributes

stickerFlowLayout.minimumInteritemSpacing = 5;

With the interitem spacing affecting the spaces between items (similar but different to the line spacing)

Using the flow layout you can save on a huge amount of code and set your collection view up programatically all in one place (just setting the inset and line spacing has immediately saved me two extra unneeded functions)

simon_smiley
  • 5,613
  • 3
  • 39
  • 54