【发布时间】: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()
}
但是当我尝试在 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 个,那么错误就会消失,但视图现在是重叠的。我觉得我缺少一些基本的东西,但不知道是什么。
这是我所希望的:
【问题讨论】:
-
你不需要复制约束,我认为删除这行每个都可以:" make.bottom.equalTo(self.productNameLabel.snp.top).inset(-8) //在这里改变”
-
我试过这个,并且在使用 inset 或 offset 的情况下遇到了类似的问题,但是使视图更接近,当我使用负数时,我得到约束错误
-
什么错误,你的单元格大小是多少,单元格大小是自动的?