ios UICollectionView 中的单元格间距 minimumInteritemSpacing

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25079305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:38:46  来源:igfitidea点击:

Cell spacing in UICollectionView minimumInteritemSpacing

iosobjective-cuicollectionviewcelluicollectionviewdelegate

提问by rams

How to do set cell spacing in a section of UICollectionView? UICollectionView there is a property minimumInteritemSpacing, I need to set 1.0 still it not work. And I implemented the delegate method.

如何在 UICollectionView 的一部分中设置单元格间距?UICollectionView 有一个属性minimumInteritemSpacing,我需要设置 1.0 仍然不起作用。我实现了委托方法。

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

回答by Matt Murdock

To check about how your collection Cells would look, you can try this in your Storyboard with the help of storyboard. First just for the sake of checking, put some static cells in your CollectionViewController like this so that your screen appears like this :

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

enter image description here

在此处输入图片说明

No you can see those cells and the spacing between them. In your case, the cells will appear with improper spacing as you have shown above. So now open with this screen open, open up the Size Inspector fron your Interface Builder. It would look something like this :

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

enter image description here

在此处输入图片说明

Now you can see some options in the size inspector window. You can adjust the size of each cell and also the spacing between them using the Min. Spacing option. And finally for equal spacing from left and right sides, use the Section Insets Option. As you change the values there, the change will be reflected in your Controller View. So you can get an idea if you want to increase/decrease some values.

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

Hope this helps.

希望这可以帮助。

回答by Mounika Vangala

you can set programmatically like this

你可以像这样以编程方式设置

UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];

layout.minimumLineSpacing = 0.0;
layout.minimumInteritemSpacing = 0.0;
contestsCollectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];

回答by Greg

This method set up spaces between cells, footer and header:

此方法在单元格、页脚和页眉之间设置空格:

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