如何删除两个UICollectionView列之间的余量
我有UICollectionView中,我想在单元格之间没有间距。 但是,尽pipe我竭尽全力,似乎无法消除这个空间,
码
- (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); }
额外的信息
- 单元格宽度是234
- UICollectionView宽度是703
从这个 。 您需要更改minimumInteritemSpacing和minimumLineSpacing 。
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; flow.itemSize = CGSizeMake(cellWidth, cellHeight); flow.scrollDirection = UICollectionViewScrollDirectionHorizontal; flow.minimumInteritemSpacing = 0; flow.minimumLineSpacing = 0; mainCollectionView.collectionViewLayout = flow;
下面对我来说是骗人的。
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; flow.itemSize = CGSizeMake(360*iPhoneFactorX, 438*iPhoneFactorX); flow.scrollDirection = UICollectionViewScrollDirectionHorizontal; flow.minimumInteritemSpacing = 0; flow.minimumLineSpacing = 0; [mainCollectionView reloadData]; mainCollectionView.collectionViewLayout = flow;
最后一行在我们分配布局的地方非常重要
你不能用默认的UICollectionViewFlowLayout来做到这一点。 尽pipe您可以使用其他布局,例如它的一个子类。 我正在使用这个类来明确设置间距:
@implementation FlowLayoutExt @synthesize maxCellSpacing; - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect]; for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) { if (nil == attributes.representedElementKind) { NSIndexPath* indexPath = attributes.indexPath; attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame; } } return attributesToReturn; } - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath]; UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset]; if (indexPath.item == 0) { // first item of section // CGRect frame = currentItemAttributes.frame; // frame.origin.x = sectionInset.left; // first item of the section should always be left aligned // currentItemAttributes.frame = frame; return currentItemAttributes; } NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section]; CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame; CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width + maxCellSpacing; CGRect currentFrame = currentItemAttributes.frame; CGRect strecthedCurrentFrame = CGRectMake(0, currentFrame.origin.y, self.collectionView.frame.size.width, currentFrame.size.height); if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) { // if current item is the first item on the line // the approach here is to take the current frame, left align it to the edge of the view // then stretch it the width of the collection view, if it intersects with the previous frame then that means it // is on the same line, otherwise it is on it's own new line CGRect frame = currentItemAttributes.frame; frame.origin.x = sectionInset.left; // first item on the line should always be left aligned currentItemAttributes.frame = frame; return currentItemAttributes; } CGRect frame = currentItemAttributes.frame; frame.origin.x = previousFrameRightPoint; currentItemAttributes.frame = frame; return currentItemAttributes; }
我通过在尺寸检查员中取消选中“ 相对于边距 ” 来解决类似的问题。
更改storyBoard中的间距或以编程方式。
要么
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 0 }
尝试将UICollectionView的属性设置为0(零) : Min Spacing For Cells and For Lines
其实,你将无法使用UICollectionViewFlowLayout
设置一个参数来实现你的目标,因为它使用单元格间距来正确alignment屏幕上的所有项目,这就是为什么他们将单元格间距设置为最小。 如果您的单元格大小是固定的,您可以使用ViewCollection
Size
以便所有单元格和边距完全适合它。