0

I was trying to remove the space between UICollectionView and this is the screenshots from device

enter image description here

I am really trying to avoid those spaces(blue color) and i have tried following codes

This is my collectionview delegate

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 30;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

First I tried this one, and its not working

- (void)viewDidLoad {
    [super viewDidLoad];
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(cell.frame.size.width, cell.frame.size.height);
    flow.scrollDirection = UICollectionViewScrollPositionCenteredVertically;
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];

}

and I tried this one also, its not working

#pragma mark collection view cell paddings
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    return 5.0;
}

Please help me to fix this.

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
Bangalore
  • 1,572
  • 4
  • 20
  • 50
  • you should use method sizeForItemAtIndexPath and layouts classes – Anton Dec 15 '15 at 06:07
  • ok let me checkout that – Bangalore Dec 15 '15 at 06:08
  • i can see only swift examples ? – Bangalore Dec 15 '15 at 06:10
  • You've set your flow delegate minimumInterimSpacing and line spacing to 0 which is good. You then need to make sure your cell size has a width of screeWidth / 2.0 (I assume you want 2 columns with no spaces). You need to conform to the UICollectionViewFlowDelegate and implement the `sizeForItemAtIndexPath:` method as @Anton pointed out. – Zhang Dec 15 '15 at 06:28

4 Answers4

2

So there is properties and methods can limit the minimum space, such as minimumInteritemSpacingForSectionAtIndex

However it is just the 'minimum space' not always the max space.

This what I am using: borrow from Cell spacing in UICollectionView

Override standard flow layout:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray *answer = [super layoutAttributesForElementsInRect:rect];

    for(int i = 1; i < [answer count]; ++i) {
        UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
        UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
        NSInteger maximumSpacing = 4;
        NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);

        if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
        }
    }
    return answer;
}

maximumSpacing could be set to any value you prefer

Community
  • 1
  • 1
Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • where should i call this? – Bangalore Dec 15 '15 at 06:24
  • You don't call it. you add a sub class of the standard flow layout, and put the code into the layout implementation. Check out `UICollectionViewFlowLayout` – Wingzero Dec 15 '15 at 06:25
  • getting error on [super layoutAttributesForElementsInRect:rect]; – Bangalore Dec 15 '15 at 06:43
  • then there is something wrong with your code. This is just a override, calling super should be super fine. read carefully and check out Cell spacing in UICollectionView. It explained well. – Wingzero Dec 15 '15 at 06:51
1

From storyboard also you can set the spacing attributes. Select your collection view - select size inspector tab, there you can adjust minimum spacing for for cells or for lines.

And you can adjust section insets also.

enter image description here

Suhail kalathil
  • 2,673
  • 1
  • 13
  • 12
  • you should notice that it's 'min' spacing, not fixed spacing. In some cases, it can be larger than it. If you met this case, checkout the code in my answer below, should help – Wingzero Dec 18 '15 at 01:57
0
flow.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);

Use sectionInset for your UICollectionViewFlowLayout in viewdidload.

  • layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0); layout.itemSize = CGSizeMake((self.view.frame.size.width/2)+4, (self.view.frame.size.width/2)+4); This is working for me. Give it a try. – Avanish Shrestha Dec 15 '15 at 06:26
0

See my answer on how to adjust UICollectionView width (using AutoLayout constraint) to fit all cells perfectly without any spacing between them.