Ios 使用tableView的图像列表中出现自动布局错误

Ios 使用tableView的图像列表中出现自动布局错误,ios,uitableview,uiimageview,autolayout,Ios,Uitableview,Uiimageview,Autolayout,我想以适合屏幕宽度的编程方式在表视图中显示远程图像列表 我的表视图具有以下约束:tableView.rowHeight=UITableViewAutomaticDimension 我创建一个单元格,添加一个图像,并创建约束 let cell = UITableViewCell() let url = URL(string: "https://static.pexels.com/photos/411089/pexels-photo-411089.jpeg") let data = NSData(c

我想以适合屏幕宽度的编程方式在表视图中显示远程图像列表

我的表视图具有以下约束:
tableView.rowHeight=UITableViewAutomaticDimension

我创建一个单元格,添加一个图像,并创建约束

let cell = UITableViewCell()
let url = URL(string: "https://static.pexels.com/photos/411089/pexels-photo-411089.jpeg")
let data = NSData(contentsOf: url!)
let imageView = UIImageView()
cell.addSubview(imageView)
imageView.image = UIImage(data: data as Data!)
imageView.translatesAutoresizingMaskIntoConstraints = false

imageView.leadingAnchor.constraint(equalTo: cell.leadingAnchor).isActive = true
imageView.trailingAnchor.constraint(equalTo: cell.trailingAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
return cell
我得到这个(部分照片):

图像渲染不正确,高度太大。我尝试修复它并添加了一个比率约束:

let ratio = imageView.image!.size.height/imageView.image!.size.width
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: ratio).isActive = true
之后,图像显示正确,但我得到一个关于约束的非致命错误:

每个图像的错误:

2017-09-10 23:50:52.490946+0300 MyHelloWorld[35126:1135489] [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:0x60800009eaa0 UIImageView:0x7fe4605236e0.height == 0.667391*UIImageView:0x7fe4605236e0.width   (active)>",
    "<NSLayoutConstraint:0x60800009ec30 H:|-(0)-[UIImageView:0x7fe4605236e0]   (active, names: '|':UITableViewCell:0x7fe4608a0c00 )>",
    "<NSLayoutConstraint:0x60800009ecd0 UIImageView:0x7fe4605236e0.trailing == UITableViewCell:0x7fe4608a0c00.trailing   (active)>",
    "<NSLayoutConstraint:0x60800009ed20 V:|-(0)-[UIImageView:0x7fe4605236e0]   (active, names: '|':UITableViewCell:0x7fe4608a0c00 )>",
    "<NSLayoutConstraint:0x60800009edc0 UIImageView:0x7fe4605236e0.bottom == UITableViewCell:0x7fe4608a0c00.bottom   (active)>",
    "<NSLayoutConstraint:0x60800009efa0 'UIView-Encapsulated-Layout-Height' UITableViewCell:0x7fe4608a0c00.height == 213.5   (active)>",
    "<NSLayoutConstraint:0x60800009eeb0 'UIView-Encapsulated-Layout-Width' UITableViewCell:0x7fe4608a0c00.width == 320   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60800009eaa0 UIImageView:0x7fe4605236e0.height == 0.667391*UIImageView:0x7fe4605236e0.width   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-09-10 23:50:52.490946+0300 MyHelloWorld[35126:1135489][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
我不知道这个约束异常到底意味着什么。我也不知道如何修复它。请帮忙,我已经花了几天时间来处理这个异常

如何修复此错误


谢谢。

您应该将约束的优先级从1000设置为999。这样,可以首先应用表视图单元的封装布局尺寸

let imageRatioConstraint = imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: ratio)
imageRatioConstraint.priority = 999
imageRatioConstraint.isActive = true

是的,修好了!非常感谢你!