How to remove the margin between two UICollectionV

2019-03-17 14:30发布

I have UICollectionView in which I would like to have no spacing between the cells. However despite my all effort I can't seem to remove the the space,

enter image description here

Code

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

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

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

Extra Info

  • Cell width is 234
  • UICollectionView width is 703

6条回答
时光不老,我们不散
2楼-- · 2019-03-17 14:45

You can't do that with the default UICollectionViewFlowLayout. Although you can use another layout, like a subclass of it. I'm using this class to set the spacing explicitly:

@implementation FlowLayoutExt
@synthesize maxCellSpacing;

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
    for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
        if (nil == attributes.representedElementKind) {
            NSIndexPath* indexPath = attributes.indexPath;
            attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
        }
    }
    return attributesToReturn;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewLayoutAttributes* currentItemAttributes =
    [super layoutAttributesForItemAtIndexPath:indexPath];

    UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset];

    if (indexPath.item == 0) { // first item of section
//        CGRect frame = currentItemAttributes.frame;
//        frame.origin.x = sectionInset.left; // first item of the section should always be left aligned
//        currentItemAttributes.frame = frame;

        return currentItemAttributes;
    }

    NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
    CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
    CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width + maxCellSpacing;

    CGRect currentFrame = currentItemAttributes.frame;
    CGRect strecthedCurrentFrame = CGRectMake(0,
                                              currentFrame.origin.y,
                                              self.collectionView.frame.size.width,
                                              currentFrame.size.height);

    if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) { // if current item is the first item on the line
        // the approach here is to take the current frame, left align it to the edge of the view
        // then stretch it the width of the collection view, if it intersects with the previous frame then that means it
        // is on the same line, otherwise it is on it's own new line
        CGRect frame = currentItemAttributes.frame;
        frame.origin.x = sectionInset.left; // first item on the line should always be left aligned
        currentItemAttributes.frame = frame;
        return currentItemAttributes;
    }

    CGRect frame = currentItemAttributes.frame;
    frame.origin.x = previousFrameRightPoint;
    currentItemAttributes.frame = frame;
    return currentItemAttributes;
}
查看更多
叛逆
3楼-- · 2019-03-17 14:47

From this. You need to change minimumInteritemSpacing and minimumLineSpacing.

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
mainCollectionView.collectionViewLayout = flow;
查看更多
beautiful°
4楼-- · 2019-03-17 14:47

Actually, you won't be able to set a parameter to achieve your goal using UICollectionViewFlowLayout because it plays with the cell spacing to properly align all items in the screen, and that's the reason why they set the cells spacing as a minimum. If your cell size are fixed you can play with the ViewCollection Size so that all cells and margins perfectly fit in it.

查看更多
小情绪 Triste *
5楼-- · 2019-03-17 14:48

Try setting 0(zero) to the properties of UICollectionView: Min Spacing For Cells and For Lines

查看更多
Viruses.
6楼-- · 2019-03-17 14:52

I have fixed similar issue by unchecking "Relative to margin" in size inspector.

enter image description here

Change the spacing in storyBoard or programmatically.

enter image description here

or

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
查看更多
劫难
7楼-- · 2019-03-17 14:56

Below did trick for me.

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(360*iPhoneFactorX, 438*iPhoneFactorX);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;


[mainCollectionView reloadData];
mainCollectionView.collectionViewLayout = flow;

Last line is very important where we are assigning the layout

查看更多
登录 后发表回答