ios中collectionview项的等间距

See*_*ker 9 ios uicollectionview

我使用集合视图控制器来显示图库中的图像.现在我打破了间距.我不能在集合视图中设置相等的间距..

- (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);
}
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(106.0f, 106.0f);
}
Run Code Online (Sandbox Code Playgroud)

这是我的代码..单元格宽度106但是imageview宽度是104.0f我在图像视图的左侧给出了2点间距现在我得到了解决方案,如下图所示. 在此输入图像描述

请帮我解决这个问题...

小智 8

要检查您的集合单元格的外观,您可以在故事板的帮助下在故事板中尝试此操作.首先只是为了检查,在你的CollectionViewController中放置一些静态单元格,这样你的屏幕就像这样:

在此输入图像描述

不,你可以看到这些细胞和它们之间的间距.在您的情况下,单元格将以不正确的间距显示,如上所示.现在打开此屏幕打开,打开Interface Builder的Size Inspector.它看起来像这样:

在此输入图像描述

现在,您可以在尺寸检查器窗口中看到一些选项.您可以使用Min调整每个单元格的大小以及它们之间的间距.间距选项.最后,从左侧和右侧使用相等的间距,使用"截面插入选项".当您更改那里的值时,更改将反映在您的控制器视图中.因此,如果您想增加/减少某些值,您可以了解一下.

希望这可以帮助.

  • 这个答案是不完整的; 值随屏幕尺寸/方向而变化. (5认同)
  • 嗨,你能告诉我怎么能以编程方式完成这个.Bcoz我用的是collectionviewcontroller我无法设置这个...... (2认同)

Moa*_*eed 7

嘿,你有答案吗?如果不是@iCoder非常好的想法,首先你可以使用界面构建器来了解我们需要多少间距.然后你可以使用这些flowLayout委托方法以编程方式设置它们.像我一样:

// 1
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *searchTerm = self.students[indexPath.row];
    // 2
    CGSize retval =  CGSizeMake(100, 100);
    retval.height = 190;
    retval.width = 170;

    return retval;
}

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

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

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section 
{
    return 20.0;
}
Run Code Online (Sandbox Code Playgroud)