代码之家  ›  专栏  ›  技术社区  ›  Wazza

应用偏移量时,CollectionView单元Snapkit约束失败

  •  0
  • Wazza  · 技术社区  · 5 年前

    我通过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()
        }
    

    但当我试图在两者之间增加空间时 imageView 以及 nameLabel 我有错误

        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个,那么错误就会消失,但视图现在会重叠。我觉得我错过了一些基本的东西,但不知道是什么。

    以下是我所希望的:

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  1
  •   Wazza    5 年前

    因此,这与单元格本身无关,而是与包含单元格的集合View的大小有关