ios UICollectionView - 单元格之间的距离?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15514526/
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
UICollectionView - distance between cells?
提问by Sarabyte Studios
I'm using a UICollectionView with the cell width of 67, right now I am using a flow layout.
我正在使用单元格宽度为 67 的 UICollectionView,现在我正在使用流布局。
I have 3 cells in a row with 30px space between cells. How can make that distance less, so that I can fit four cells in a row? Does this method have something to do with it?
我有 3 个单元格,单元格之间有 30px 的空间。如何缩短该距离,以便我可以连续放置四个单元格?这个方法和它有关系吗?
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 10, 10, 10);
}
回答by Anil Varghese
This UICollectionViewFlowLayoutDelegatemethod will help you..
这个 UICollectionViewFlowLayoutDelegate方法可以帮到你。。
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10; // This is the minimum inter item spacing, can be more
}
One more if you need more space to arrange your cells adjust the edgeInsetsalso
还有一个,如果你需要更多的空间来安排你的细胞调节edgeInsets也
回答by Aamir
You can configure spacing between cells or rows by using following properties.
1) For setting space between rows.
您可以使用以下属性配置单元格或行之间的间距。
1) 用于设置行之间的空间。
[self.collectionView setMinimumLineSpacing:5];
2) For setting space between items/cells.
2) 用于设置项目/单元格之间的空间。
[self.collectionView setMinimumInteritemSpacing:5];
You can also configure it from InterfaceBuilder(IB). Just select UICollectionView from storyboard/Xib file and click in Size Inspectoras specified in below image. 
您也可以从 InterfaceBuilder(IB) 配置它。只需从故事板/Xib 文件中选择 UICollectionView 并单击下图指定的大小检查器。 
回答by Hsm
From this. You need to change minimumInteritemSpacingand minimumLineSpacing.
从这个。您需要更改minimumInteritemSpacing和minimumLineSpacing。
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
mainCollectionView.collectionViewLayout = flow;
回答by matt
No, it doesn't. Just change the layout's minimumInteritemSpacing.
不,它没有。只需更改布局的minimumInteritemSpacing.
回答by Tanvi Jain
You can use delegate method like:- for changing the space between cells in a section.
您可以使用以下委托方法:-用于更改部分中单元格之间的空间。
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
For showing top,bottom y padding between cells of different sections.
用于显示不同部分的单元格之间的顶部、底部 y 填充。
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
// Layout: Set Edges //setting insets for cell
// Layout: Set Edges //设置单元格的插入
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//top,left,botttom,right
回答by MAhipal Singh
swift 3.0
迅捷 3.0
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
if DEVICE_IDIOM == .pad {
return 15.0
}
else {
return 10.0
}
}
回答by Felipe Braz
I had the same issue..
我遇到过同样的问题..
In my case I needed a retangular cell.
就我而言,我需要一个矩形单元格。
To achieve the desired result I used the UICollectionViewDelegateFlowLayout.
为了达到预期的结果,我使用了UICollectionViewDelegateFlowLayout。
This is my implementation of one of the method:
这是我对其中一种方法的实现:
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
{
CGFloat width = CGRectGetWidth(self.view.frame)/3.0f;
return CGSizeMake(width, (width * 1.1f));
}
This worked fine for me! I hope it helps someone.
这对我来说很好用!我希望它可以帮助某人。
回答by Lakhdeep Singh
Swift 4.2
斯威夫特 4.2
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}

