更新高度后无法同时满足约束

时间:2017-01-12 21:31:07

标签: ios swift uitableview autolayout nslayoutconstraint

我有一个包含两个图像视图的单元格: Cell

小图像视图的约束: Small

对大人物的限制: Big

按钮的约束: Button

大图像视图有"缩放到填充"内容模式。

在我用SDWebImage加载之前我不知道图像的大小,所以我在代码中创建了恒定的高度约束及其出口。

加载图片后,我改变高度约束的常数以保持图像的方面:

imageIv.sd_setImage(with: feedAd.getImageUrl(), completed: { (image: UIImage?, error: Error?, cacheType: SDImageCacheType!, imageURL: URL?) in
                if image != nil {
                    let ratio = image!.size.height / image!.size.width
                    self.imageHeightConstraint.constant = self.imageIv.frame.width * ratio
                    self.updateConstraints()
                } else {
                    print("Image == nil!")
                }

                if self.imageViewLoadedCallback != nil {
                    self.imageViewLoadedCallback!()
                }
            })

然后我看到以下内容:

2017-01-13 00:21:10.387862 DotaTo[1541:503878] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x17008df70 UIImageView:0x102d39000.height == 48>",
    "<NSLayoutConstraint:0x174098bf0 UIImageView:0x102e59e40.height == 228.667>",
    "<NSLayoutConstraint:0x17409bc10 UIImageView:0x102d39000.top == UITableViewCellContentView:0x102d4bae0.top + 8>",
    "<NSLayoutConstraint:0x174098010 UIImageView:0x102e59e40.top == UIImageView:0x102d39000.bottom + 8>",
    "<NSLayoutConstraint:0x17409aae0 UIButton:0x102e6c600.top == UIImageView:0x102e59e40.bottom + 16>",
    "<NSLayoutConstraint:0x17409aa40 UITableViewCellContentView:0x102d4bae0.bottom == UIButton:0x102e6c600.bottom + 8>",
    "<NSLayoutConstraint:0x17009be40 UITableViewCellContentView:0x102d4bae0.height == 293>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x174098bf0 UIImageView:0x102e59e40.height == 228.667>

单元格高度未更新,因此视图是高度切割的。但是当我向下滚动表格视图时,除非此单元格离开屏幕然后滚动回到它,它会正确调整大小。

所以,我有两个问题:

  1. 如何解决此错误?
  2. 如何让细胞调整它的高度 正确?

0 个答案:

没有答案