【问题标题】:How to remove the margin between two UICollectionView columns如何删除两个 UICollectionView 列之间的边距
【发布时间】:2014-02-12 10:11:04
【问题描述】:

我有 UICollectionView,我希望单元格之间没有间距。然而,尽管我尽了最大的努力,我似乎​​无法删除空间,

代码

- (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

【问题讨论】:

  • 提示在方法名称中,这是项目之间的“最小”间距。 CollectionView 将决定差距有多大。我认为您将需要创建自己的 UICollectionViewLayout
  • 滚动方向是什么?
  • 我不确定月光骑士是否正确。我有一个工作示例,绝对基本,从 iPad 的单视图应用程序模板创建,使用您的尺寸,具有默认的流布局,显示没有间隙是可能的,并且可以用最少的...您包含的委托方法进行控制更多。您是否记录了该代码以验证它是否被调用?

标签: ios objective-c ipad cocoa-touch uicollectionview


【解决方案1】:

来自this。 您需要更改 minimumInteritemSpacingminimumLineSpacing

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
mainCollectionView.collectionViewLayout = flow;

【讨论】:

    【解决方案2】:

    下面对我有用。

    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;
    

    最后一行在我们分配布局时非常重要

    【讨论】:

      【解决方案3】:

      我通过在尺寸检查器中取消选中“Relative to margin”解决了类似的问题。

      更改故事板中的间距或以编程方式。

         func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
              return 0
          }
      

      【讨论】:

        【解决方案4】:

        你不能用默认的 UICollectionViewFlowLayout 来做到这一点。尽管您可以使用其他布局,例如它的子类。我正在使用这个类来明确设置间距:

        @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;
        }
        

        【讨论】:

          【解决方案5】:

          尝试将 0(zero) 设置为 UICollectionView 的属性:Min Spacing For Cells and For Lines

          【讨论】:

            【解决方案6】:

            实际上,您将无法使用UICollectionViewFlowLayout 设置参数来实现您的目标,因为它使用单元格间距来正确对齐屏幕中的所有项目,这就是他们将单元格间距设置为的原因最低限度。 如果您的单元格大小是固定的,您可以使用 ViewCollection Size 以便所有单元格和边距都完美地适合它。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-06-13
              • 2012-04-07
              • 2012-12-07
              • 2017-03-09
              • 2014-11-18
              • 1970-01-01
              • 2017-04-03
              • 2020-01-20
              相关资源
              最近更新 更多