【问题标题】:CollectionView cell Snapkit constraints fail when applying offset应用偏移量时,CollectionView 单元格 Snapkit 约束失败
【发布时间】:2021-06-20 08:13:06
【问题描述】:

我通过 SnapKit 将以下约束应用于我的 Cell,它们工作正常。

    self.imageView.snp.makeConstraints { make in
      make.leading.top.trailing.equalToSuperview()
      make.bottom.equalTo(self.nameLabel.snp.top)
      if let width = self.imageView.image?.size.width {
          make.height.equalTo(width)
      }
    }

    self.nameLabel.snp.makeConstraints { make in
      make.leading.trailing.equalToSuperview()
      make.top.equalTo(self.imageView.snp.bottom)
      make.bottom.equalTo(self.priceLabel.snp.top)
    }

    self.priceLabel.snp.makeConstraints { make in
      make.top.equalTo(self.nameLabel.snp.bottom)
      make.leading.trailing.bottom.equalToSuperview()
    }

但是当我尝试在 imageViewnameLabel 之间添加空格时,我得到了错误

    self.productImageView.snp.makeConstraints { make in
      make.leading.top.trailing.equalToSuperview()
      make.bottom.equalTo(self.productNameLabel.snp.top).inset(-8) // change here
      if let width = self.productImageView.image?.size.width {
          make.height.equalTo(width)
      }
    }

    self.productNameLabel.snp.makeConstraints { make in
      make.leading.trailing.equalToSuperview()
      make.top.equalTo(self.productImageView.snp.bottom).offset(8) // change here
      make.bottom.equalTo(self.productPriceLabel.snp.top)
    }

    self.productPriceLabel.snp.makeConstraints { make in
      make.top.equalTo(self.productNameLabel.snp.bottom)
      make.leading.trailing.bottom.equalToSuperview()
    }

我不明白的是,如果我将约束中的减号移到其他 8 个,那么错误就会消失,但视图现在是重叠的。我觉得我缺少一些基本的东西,但不知道是什么。

这是我所希望的:

【问题讨论】:

  • 你不需要复制约束,我认为删除这行每个都可以:" make.bottom.equalTo(self.productNameLabel.snp.top).inset(-8) //在这里改变”
  • 我试过这个,并且在使用 inset 或 offset 的情况下遇到了类似的问题,但是使视图更接近,当我使用负数时,我得到约束错误
  • 什么错误,你的单元格大小是多少,单元格大小是自动的?

标签: swift snapkit


【解决方案1】:

所以这与单元格本身无关,而是包含单元格的 collectionView 的大小

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2017-12-10
    • 2019-03-31
    • 2015-04-24
    相关资源
    最近更新 更多