iOS设置为0时,UICollectionView间距仍然存在 – 如何设置单元格之间没有间距

我有一个简单的UICollectionView,我已经在InterfaceBuilder中设置了0的间距,但是当我用单元填充集合视图时,仍然有一些间距。 有没有什么特别的,并不是很明显,我需要做的,以实际上看到一个0间距collectionview单元格超出设置它有0间距? 谢谢。

编辑*一些代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor clearColor]; UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, cell.frame.size.width -4, cell.frame.size.height -4)]; lbl.backgroundColor = [UIColor clearColor]; lbl.font = [UIFont boldSystemFontOfSize:20]; lbl.text = [NSString stringWithFormat:@"$%0.0f", [[amountsArray objectAtIndex:indexPath.row] floatValue]]; lbl.textAlignment = NSTextAlignmentCenter; lbl.layer.borderWidth = 1; [cell addSubview:lbl]; [lbl release]; return cell; } 

在这里输入图像说明

查询的简单解决scheme。 在你的viewController的.m文件中join这个:

 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { ProductDetailViewController *HomeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ProductDetailView"]; HomeVC.title = @"BrianDemo"; [self.navigationController pushViewController:HomeVC animated:YES]; } - (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 0.0; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0.0; } 

你必须创build自定义的UICollectionViewLayout

单元之间的空间将等于cellSpacing

 import UIKit class CustomViewFlowLayout : UICollectionViewFlowLayout { let cellSpacing:CGFloat = 0 override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { if let attributes = super.layoutAttributesForElementsInRect(rect) { for (index, attribute) in attributes.enumerate() { if index == 0 { continue } let prevLayoutAttributes = attributes[index - 1] let origin = CGRectGetMaxX(prevLayoutAttributes.frame) if(origin + cellSpacing + attribute.frame.size.width < self.collectionViewContentSize().width) { attribute.frame.origin.x = origin + cellSpacing } } return attributes } return nil } } 

我解决了这个问题,得到了我想要的布局,如下所示:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor clearColor]; //clear any contents on the cell for (UIView *subView in [cell subviews]) { [subView removeFromSuperview]; } //Label to put on the cell UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(2, 2, cell.frame.size.width -4, cell.frame.size.height -4)]; lbl.backgroundColor = [UIColor clearColor]; lbl.textColor = [UIColor colorWithRed:[CPExtras RGBtoPercent:70] green:[CPExtras RGBtoPercent:92] blue:[CPExtras RGBtoPercent:105] alpha:1]; lbl.font = [UIFont boldSystemFontOfSize:20]; lbl.text = @"100"; lbl.textAlignment = NSTextAlignmentCenter; //Give the cell a border cell.layer.borderColor = [[UIColor colorWithRed:[CPExtras RGBtoPercent:70] green:[CPExtras RGBtoPercent:92] blue:[CPExtras RGBtoPercent:105] alpha:0.5] CGColor]; cell.layer.borderWidth = 0.5; [cell addSubview:lbl]; [lbl release]; return cell; } 

在IB我有这些测量设置为collectionview:

收藏查看大小

集合视图流布局大小

Swift 3版本的@MihirOza的解决scheme

适用于水平和垂直集合视图

 // removing spacing func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 0.0 } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0.0 } 

[UICollectionViewFlowLayout minimumInteritemSpacing]的文档中提到:

这个间距用于计算一条线上可以放入多less物品,但是在物品数量确定之后,实际间距可能会向上调整。

您可能需要实现自定义布局来执行此操作。 文档可以在这里find,并在这里的例子。

为了实际上有零空间,单元格的数量和宽度应该可以被集合视图自己的宽度整除,例如,如果一次有5个单元格,宽度为100px,那么您的集合视图的宽度应该是500px ,如果它更大,那么它将强制单元之间的空间。