【问题标题】:Cell is in the center of collection view when there is only one cell当只有一个单元格时,单元格位于集合视图的中心
【发布时间】:2019-09-03 11:07:00
【问题描述】:

我想从左到右排列单元格。所以我使用UICollectionViewFlowLayout:

UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// use this for let cell width fit for label width
layout.estimatedItemSize = CGSizeMake(80, 30);
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];

单元格的宽度适合标签的宽度:

// cell code
@implementation CQGoodsSearchCell

- (void)setupUI {
    self.contentView.layer.cornerRadius = 15;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.backgroundColor = [UIColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.00];

    self.nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    self.nameLabel.textColor = [UIColor colorWithRed:0.18 green:0.18 blue:0.18 alpha:1.00];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(10);
        make.right.mas_offset(-10);
    }];
}

当有多个单元格时一切顺利:

但是,当只有一个单元格时,单元格位于中心:

如果我使用itemSize而不是estimatedItemSize,就可以了:

layout.itemSize = CGSizeMake(80, 30);

我真的很困惑。将单元格放在集合视图的中心不是我想要的,但我也想使用自动布局,以便单元格的宽度自行调整。

那么有什么办法可以避免呢?

【问题讨论】:

    标签: ios objective-c uicollectionview autolayout


    【解决方案1】:

    基本上这与我在这里讨论的问题相同:

    https://stackoverflow.com/a/52428617/341994

    总之,您尝试使用的功能,自动调整大小的集合视图单元格,不起作用并且从未起作用。这就是为什么,正如你所说的,

    如果我使用itemSize而不是estimatedItemSize,就可以了

    没错!这不是你的错误;这是一个iOS错误。自行调整大小的集合视图单元不起作用。应用程序崩溃或流程布局不正确。

    这就是解决方案。不要使用estimatedItemSize。相反,自己计算正确的大小并在您的实现中提供它

    func collectionView(_ collectionView: UICollectionView, 
        layout collectionViewLayout: UICollectionViewLayout, 
        sizeForItemAt indexPath: IndexPath) -> CGSize {
            // calculate/obtain correct size and return it
    }
    

    【讨论】:

      【解决方案2】:

      另外,使用自定义UICollectionViewLayout可以解决这个问题:

      对于 Objective-C:

      @interface CQLeftSideLayout : UICollectionViewFlowLayout
      
      @end
      
      @implementation CQLeftSideLayout
      
      - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
      
          NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
      
          if (attributes.count == 1) {
              UICollectionViewLayoutAttributes *currentAttributes = attributes.firstObject;
              currentAttributes.frame = CGRectMake(self.sectionInset.left, currentAttributes.frame.origin.y, currentAttributes.frame.size.width, currentAttributes.frame.size.height);
          }
      
          return attributes;
      
      }
      
      @end
      

      对于 Swift:

      class LeftSideLayout: UICollectionViewFlowLayout {
      
          override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
      
              let attributes = super.layoutAttributesForElements(in: rect)
      
              if attributes?.count == 1 {
      
                  if let currentAttribute = attributes?.first {
                      currentAttribute.frame = CGRect(x: self.sectionInset.left, y: currentAttribute.frame.origin.y, width: currentAttribute.frame.size.width, height: currentAttribute.frame.size.height)
                  }
      
              }
      
      
              return attributes
      
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        在估计大小选项中选择none。如果集合视图只有一项,它将左对齐而不是居中。

        【讨论】:

        • 为我工作,谢谢!
        猜你喜欢
        • 2020-07-30
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多