0

There's a huge space between the cells in the collectionView as shown in the image below. How can i reduce it.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"SETTING SIZE FOR ITEM AT INDEX %ld", (long)indexPath.row);
    CGSize mElementSize = CGSizeMake(100, 50);
    return mElementSize;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

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

// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
    return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
}


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

enter image description here

UPDATE

enter code here

Update

enter image description here

Illep
  • 16,375
  • 46
  • 171
  • 302

3 Answers3

0

You should be able to use the identity inspector to adjust the spaces straight from the Xib, does adjusting the "Min Spacing" or "Section Inserts" help?

enter image description here

Ollie
  • 1,926
  • 1
  • 20
  • 35
0

You can adjust the size of the UICollectionViewCell depending on your screen size to reduce the gap between two UICollectionViewCells.

Update 1

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
      return CGSizeMake([UIScreen mainScreen].bounds.size.width/4, 300);
      // A total of 4 views displayed at a time,  we divide width / 4, 
      // and cell will automatically adjust its size.
}

HTH Enjoy Coding!!

Catalina T.
  • 3,456
  • 19
  • 29
Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
0

Create a custom class for collectionView flowlayout and set flowlayout in IB with custom class

@interface NDCollectionViewFlowLayout : UICollectionViewFlowLayout

@end

// Created by Nick Snyder on 11/13/12. // https://gist.github.com/nicksnyder/4075682 // UICollectionView flowLayout not wrapping cells correctly (iOS) // NDCollectionViewFlowLayout.m

#import "NDCollectionViewFlowLayout.h"

@implementation NDCollectionViewFlowLayout

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
  NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
  NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
  for (UICollectionViewLayoutAttributes *attribute in attributes) {
    if ((attribute.frame.origin.x + attribute.frame.size.width <= self.collectionViewContentSize.width) &&
        (attribute.frame.origin.y + attribute.frame.size.height <= self.collectionViewContentSize.height)) {
      [newAttributes addObject:attribute];
    }
  }
  return newAttributes;
}

@end

enter image description here enter image description here

Community
  • 1
  • 1
Ram Vadranam
  • 485
  • 5
  • 14