繁体   English   中英

减少UICollectionView中的项目间距

[英]Reduce interitem spacing in UICollectionView

我已经查了一下:

UICollectionView单元格之间的距离?

UICollectionView中的单元格间距

减少UICollectionViewCells之间的空间

但他们都没有为我工作。

我也尝试实现UICollectionViewDelegateFlowLayout并实现了

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

但它对此没有影响。

请查看以下在iPhone 7 / iPhone 5s / iPhone 7 Plus中拍摄的屏幕截图(图片顺序相同)

iPhone 7

iPhone 5S

iPhone 7 Plus

我还查看了自动调整大小的单元格,但我不希望使用自动调整大小的单元格,因为我的商品大小始终是固定的并且可以满足我的要求。

我还设置了情节提要中单元格最小间距,但它对布局没有影响。

如何减少两个单元格之间的较大间隙(项目间距),避免夹在iPhone 5S中以及iPhone 7 Plus右侧的空白处(背景为白色,因此无法看到)

任何见解将不胜感激。

如果您的像元大小为“固定”,则无法避免该间隙,因为您已硬编码了固定宽度!

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

(计算像元大小)

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 )?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM