0

Objective

I am trying to decrease the space between my UICollectionViewCells

What I tried

I tried subclassing UICollectionViewFlowLayout and overriding this method:

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

    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;
}

and then in my UICollectionViewController viewDidLoad:

SublassedcollectionViewLayout *space = [[SublassedcollectionViewLayout alloc]init];
self.collectionView.collectionViewLayout = space;

Problem

That just makes my UICollectionViewCells smaller.

EDIT: I want to decrease the spacing from left to right instead of top to bottom.

Any help will be appreciated!

Abdullah Shafique
  • 6,878
  • 8
  • 35
  • 70
  • possible duplicate of [Placing a margin around each edge of the UICollectionView](http://stackoverflow.com/questions/16934831/placing-a-margin-around-each-edge-of-the-uicollectionview) – Sean Kladek Jan 12 '14 at 03:50
  • @skladek I am not trying to put margins on the cell.I am trying to decrease the space between one cell and another. – Abdullah Shafique Jan 12 '14 at 12:14
  • Which will be accomplished by either increasing the size of your cell or adding margins on the left and right. Collection views work by taking the full width, subtracting margins from each side, then evenly spacing the elements with the remaining space. If you want cells closer together, you need to decrease that remaining space. – Sean Kladek Jan 13 '14 at 01:42

1 Answers1

6

VERTICAL SPACING

Select your collection view in the storyboard.

Then change...

enter image description here

Changing

enter image description here

To

enter image description here

I also found this answer, might also be useful

HORIZONTAL SPACING

For Left to Right Spacing, from my testing you can only change the distance from the screen edge to first cell, called Section Insets. You can also change the min spacing between the cells, from 0 to x using Min Spacing For Cells

Setting the collection view to these settings produces

enter image description here

enter image description here

Changing the Min Cell Spacing to:

enter image description here

enter image description here

From this, it appears that you can only adjust the insets on the left and right (padding) from the first and last cell and then the minimum distance between cell. The rest is automatically calculated.

So to get the desired effect you need to alter these values, Min Spacing For Cells and the Section Insets

Community
  • 1
  • 1
DogCoffee
  • 19,820
  • 10
  • 87
  • 120