减lesscollectionView中的单元格之间的空间

collectionViewcells之间有一个巨大的空间,如下图所示。 我怎样才能减less它。

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"SETTING SIZE FOR ITEM AT INDEX %ld", (long)indexPath.row); CGSize mElementSize = CGSizeMake(100, 50); return mElementSize; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 2.0; } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 2.0; } // Layout: Set Edges - (UIEdgeInsets)collectionView: (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { // return UIEdgeInsetsMake(0,8,0,8); // top, left, bottom, right return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 10; } 

在这里输入图像说明

UPDATE

在此处输入代码

更新

在这里输入图像说明

您应该能够使用身份检查员直接从Xib调整空格,调整“Min Spacing”或“Section Inserts”帮助吗?

在这里输入图像说明

为collectionView flowlayout创build一个自定义类,并使用自定义类在IB中设置flowlayout

 @interface NDCollectionViewFlowLayout : UICollectionViewFlowLayout @end 

//由Nick Snyder在11/13/12创build。 // https://gist.github.com/nicksnyder/4075682 // UICollectionView flowLayout没有正确包装单元格(iOS) // NDCollectionViewFlowLayout.m

 #import "NDCollectionViewFlowLayout.h" @implementation NDCollectionViewFlowLayout - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count]; for (UICollectionViewLayoutAttributes *attribute in attributes) { if ((attribute.frame.origin.x + attribute.frame.size.width <= self.collectionViewContentSize.width) && (attribute.frame.origin.y + attribute.frame.size.height <= self.collectionViewContentSize.height)) { [newAttributes addObject:attribute]; } } return newAttributes; } @end 

在这里输入图像说明在这里输入图像说明

您可以根据您的屏幕大小调整UICollectionViewCell大小,以缩小两个UICollectionViewCells之间的差距。

更新1

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake([UIScreen mainScreen].bounds.size.width/4, 300); // A total of 4 views displayed at a time, we divide width / 4, // and cell will automatically adjust its size. } 

HTH享受编码!