减少 UICollectionView 中的项目间距

Reduce interitem spacing in UICollectionView

我已经查过了:-

UICollectionView distance between cells?

Cell spacing in UICollectionView

Decrease Space between UICollectionViewCells

但其中 none 对我有用。

我还尝试实现了 UICollectionViewDelegateFlowLayout 并实现了

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 3
}

但对它没有影响

请看下面在iPhone7/iPhone5s/iPhone7Plus(图序相同)

截取的截图

我也看过自动调整大小的单元格,但我不想要自动调整大小的单元格,因为我的项目大小总是固定的并且符合我的要求。

我还在故事板中设置了单元格的最小间距,但它对布局没有影响。

如何减少两个单元格之间的大间隙(项间距),避免在iPhone 5S 和[=51= 右侧的空白区域进行裁剪] 7 Plus(因为背景是白色所以看不到)

任何见解将不胜感激。

如果你的单元格大小是 "Fixed" ,你就无法避免间隙,因为你硬编码了一个固定的宽度!

您需要计算 collectionView.frame 的总宽度并将其除以 2,还要包括项目之间的间距,而不是 "fixed" 大小。快速输入示例:

(计算像元大小)

CGFloat spacing = 3.0f
CGFloat itemWidth = collectionView.frame.size.width / 2 - spacing
CGSize totalSize = itemWidth, itemWidth

尝试在sizeForItemAtIndexPath方法中设置每个单元格的widthheight

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    float cellWidth = screenWidth / 2.0; //Replace the divisor with the column count requirement. Make sure to have it in float.

    CGFloat screenHeight = screenRect.size.height;
    screenHeight = screenHeight - 114.0; // Sum of Bottom View & Top View Height

    float cellHeight = screenHeight /(totalArray.count/2);  //totalArray contains item used to show in collectionview

    if(DEFAULT_CELL_HEIGHT > cellHeight){
        cellHeight=DEFAULT_CELL_HEIGHT;
    }

    CGSize size = CGSizeMake(cellWidth, cellHeight);

    return size;
}

@Hauzin 的回答很好,可以消除不同设备中的间距。对于单元格之间的间距(minimumInteritemSpacingForSectionAt),您是否使视图控制器符合此协议(UICollectionViewDelegateFlowLayout)?