布局约束 - 无法同时满足约束条件

4

我有一个应用程序,当我点击我的个人资料页面时,我的控制台中一直出现这些错误

我已经尝试过

enter image description here

这是我个人资料页面上的全部限制条件。

enter image description here

我一直收到

enter image description here

john
jane
andrew
2018-07-31 11:58:46.821640-0400 tableInTabbed[4355:5682078] [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:0x604000281130 UIImageView:0x7ff91cf1d030.width == 70   (active)>",
    "<NSLayoutConstraint:0x6040002812c0 UIImageView:0x7ff91cf1d030.width == UIImageView:0x7ff91cf1d030.height   (active)>",
    "<NSLayoutConstraint:0x604000280d20 UITableViewCellContentView:0x7ff91cf1c7f0.bottomMargin == UIImageView:0x7ff91cf1d030.bottom + 52.67   (active)>",
    "<NSLayoutConstraint:0x6040002815e0 UIImageView:0x7ff91cf1d030.top == UITableViewCellContentView:0x7ff91cf1c7f0.topMargin + 29.33   (active)>",
    "<NSLayoutConstraint:0x60c000290040 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff91cf1c7f0.height == 168   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6040002812c0 UIImageView:0x7ff91cf1d030.width == UIImageView:0x7ff91cf1d030.height   (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.
2018-07-31 11:58:46.822304-0400 tableInTabbed[4355:5682078] [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:0x604000280d20 UITableViewCellContentView:0x7ff91cf1c7f0.bottomMargin == UIImageView:0x7ff91cf1d030.bottom + 52.67   (active)>",
    "<NSLayoutConstraint:0x6040002815e0 UIImageView:0x7ff91cf1d030.top == UITableViewCellContentView:0x7ff91cf1c7f0.topMargin + 29.33   (active)>",
    "<NSLayoutConstraint:0x604000281310 UIImageView:0x7ff91cf1d030.height == 70   (active)>",
    "<NSLayoutConstraint:0x60c000290040 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff91cf1c7f0.height == 168   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x604000281310 UIImageView:0x7ff91cf1d030.height == 70   (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.
2018-07-31 11:58:46.822846-0400 tableInTabbed[4355:5682078] [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:0x60c00028fa00 UIImageView:0x7ff91cc140c0.width == 70   (active)>",
    "<NSLayoutConstraint:0x60c00028ff00 UIImageView:0x7ff91cc140c0.width == UIImageView:0x7ff91cc140c0.height   (active)>",
    "<NSLayoutConstraint:0x60c00028a8c0 UITableViewCellContentView:0x7ff91cc13cb0.bottomMargin == UIImageView:0x7ff91cc140c0.bottom + 52.67   (active)>",
    "<NSLayoutConstraint:0x60c000289b50 UIImageView:0x7ff91cc140c0.top == UITableViewCellContentView:0x7ff91cc13cb0.topMargin + 29.33   (active)>",
    "<NSLayoutConstraint:0x60c000290900 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff91cc13cb0.height == 168   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c00028ff00 UIImageView:0x7ff91cc140c0.width == UIImageView:0x7ff91cc140c0.height   (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.
2018-07-31 11:58:46.823293-0400 tableInTabbed[4355:5682078] [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:0x60c00028a8c0 UITableViewCellContentView:0x7ff91cc13cb0.bottomMargin == UIImageView:0x7ff91cc140c0.bottom + 52.67   (active)>",
    "<NSLayoutConstraint:0x60c000289b50 UIImageView:0x7ff91cc140c0.top == UITableViewCellContentView:0x7ff91cc13cb0.topMargin + 29.33   (active)>",
    "<NSLayoutConstraint:0x60c00028fb90 UIImageView:0x7ff91cc140c0.height == 70   (active)>",
    "<NSLayoutConstraint:0x60c000290900 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff91cc13cb0.height == 168   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c00028fb90 UIImageView:0x7ff91cc140c0.height == 70   (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.
2018-07-31 11:58:46.823961-0400 tableInTabbed[4355:5682078] [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:0x60c000290ae0 UIImageView:0x7ff91cc16a40.width == 70   (active)>",
    "<NSLayoutConstraint:0x60c000290b30 UIImageView:0x7ff91cc16a40.width == UIImageView:0x7ff91cc16a40.height   (active)>",
    "<NSLayoutConstraint:0x60c000290cc0 UITableViewCellContentView:0x7ff91cc16840.bottomMargin == UIImageView:0x7ff91cc16a40.bottom + 52.67   (active)>",
    "<NSLayoutConstraint:0x60c000290d60 UIImageView:0x7ff91cc16a40.top == UITableViewCellContentView:0x7ff91cc16840.topMargin + 29.33   (active)>",
    "<NSLayoutConstraint:0x6040002822b0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff91cc16840.height == 168   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c000290b30 UIImageView:0x7ff91cc16a40.width == UIImageView:0x7ff91cc16a40.height   (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.
2018-07-31 11:58:46.824333-0400 tableInTabbed[4355:5682078] [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:0x60c000290cc0 UITableViewCellContentView:0x7ff91cc16840.bottomMargin == UIImageView:0x7ff91cc16a40.bottom + 52.67   (active)>",
    "<NSLayoutConstraint:0x60c000290d60 UIImageView:0x7ff91cc16a40.top == UITableViewCellContentView:0x7ff91cc16840.topMargin + 29.33   (active)>",
    "<NSLayoutConstraint:0x60c000290b80 UIImageView:0x7ff91cc16a40.height == 70   (active)>",
    "<NSLayoutConstraint:0x6040002822b0 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff91cc16840.height == 168   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c000290b80 UIImageView:0x7ff91cc16a40.height == 70   (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.

这是我拥有的

//
//  ProfileViewController
//  tableInTabbed
//
//  Copyright © 2018 LR Web Design. All rights reserved.
//

import UIKit

class ProfileViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    let profiles = ["john", "jane", "andrew"]
    let devicesInProfile = ["3", "5", "10"]

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return (profiles.count)
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
        cell.profileImg.image = UIImage(named: profiles[indexPath.row] + ".jpg")
        cell.profileName.text = profiles[indexPath.row]

        print(profiles[indexPath.row])

        return cell

    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

} 

我只想摆脱错误消息。
更新
@Sandeep,尝试了您的答案后,我最终得到了这个约束条件。

enter image description here

这是结果

enter image description here

你能再提供一点建议吗?


我没有看到任何来自UIImageView或UILabel的前导或尾随约束。在UIImageView中添加前导约束,在标签中添加尾随约束,这应该解决您的问题。至于约束中断的问题 - Sandeep Bhandari
1
控制台日志非常清晰,您的图像视图具有高度约束为70,并且您对单元格进行了顶部和底部约束,值分别为29.3和57.2,这意味着单元格的高度应为70 + 29.3 + 57.2。如果您已经实现了IndexPath中的行高,请确保返回正确的值,否则请使用自动单元格高度(UITableViewAutomaticDimension)。 - Sandeep Bhandari
1
据我所见,您的tableView数据源已经为tableView提供了高度为168,而您现有约束的计算结果为70 + 29.3 + 57.2 = 156.5。因此,在渲染时,实际提供的高度与您的组件提供的高度存在差异。因此,iOS打破了UIIMageView的高度约束以正确呈现它。您可能会发现,自动布局约束只是简单的数学方程式。尝试分析它们,您就可以解决错误。 - Sandeep Bhandari
1
UILabel看起来没问题,我觉得我的图片有问题。 - code-8
1
让我加上带有限制条件的答案 :) - Sandeep Bhandari
显示剩余10条评论
3个回答

2

步骤1:

将一个View拖到单元格上,并对视图应用水平和垂直居中约束(这将显示一些约束断裂,但稍等一下)

enter image description here

步骤2:

将imageView拖到视图上,并按照所示应用高度约束/纵横比/前导/高度和底部约束

enter image description here

步骤3:

将UILabel拖到视图上,最好是ImageView的右侧,并将尾随约束和前导约束应用到标签上。前导约束将是单元格的内容视图,而前导约束将是UIImageView。

标签的尾随约束

enter image description here

现在添加UIImageView的前导约束(从标签到imageView控制拖动并选择水平间距)

enter image description here

最后再次从标签到imageView控制拖动并选择垂直居中

enter image description here

就是这样了 :)

如果一切正常,最终约束应该如下所示

enter image description here

希望能对你有所帮助


1
哇,我必须说,这个答案太棒了!我已经按照指示做完并且运行成功了。 - code-8
@kyo:很高兴我能帮到你 :) 如果有帮助,请考虑接受答案 :) - Sandeep Bhandari
这是我为标签设计的最终外观。https://pasteboard.co/Hx23u5c.png - code-8
1
@kyo:看起来你在标签和UIImageView之间设置了非常大的水平间距约束,将其减小到8,你会发现名称完全打印并位于屏幕中央,请确保你已经摆脱了所有的红线和警告。在我的情况下,我的xib中没有任何警告或错误。 - Sandeep Bhandari
让我根据您最后的评论尝试一下。同时,我会根据您的建议更新我的帖子结果。 - code-8
显示剩余6条评论

1
你在代码中所做的似乎很好,但与你在控制台中看到的无关。
首先,你应该知道红色约束意味着有错误,更具体地说,这意味着界面构建器无法识别要实现的内容,因为至少缺少一个约束条件(这就是为什么你在控制台上看到这些投诉)。
因此,我强烈建议避免采用“添加缺失约束”或“重置为建议的约束”方法,这不是处理约束的优雅选项。大多数情况下,即使他们没有添加红色(缺失)约束,也不会导致预期结果,这似乎是合理的,因为它不可能如此神奇地读取你的思想并将其转换,可能有多个方面来确定如何设置约束!相反,自己动手做,这是实现确切外观/行为的好方法。请记住,这不仅是一种静态的方法,在理解了自动布局的工作原理后,你会发现有多种方法可以实现它。
总的来说,预期结果是看到蓝色线条(约束),而不是红色或橙色。

现在,因为描述如何设置约束(自动布局)可能过于宽泛,我建议查看来自苹果的《了解自动布局》,这是一个很好的入门指南。


0

1- 确保只有从顶部到底部的一个元素路径连接到contentView,如果看到红线,请断开底部约束并更改中间项约束,然后重新连接

2- 将最底部的约束优先级设置为999

3- 将imageView的leading设置为只有最底部的元素连接到contentView的底部

//

这里的蓝线应该只是与img宽度和高度有关的约束条件 输入图像描述

//

在viewDidLoad中设置

tableView.estimatedRowHeight = 200;

tableView.rowHeight = UITableViewAutomaticDimension;

我不明白你的图表。蓝线是什么意思?请添加更多细节,以便我能够理解你的意思。 - code-8
我在添加你的两行代码时出现了错误。请查看此处:https://pasteboard.co/Hx1QqzS.png - code-8
@Sh_Khan,请您检查一下最后的建议?为什么有这么多红色的限制条件? - code-8
@AhmadF 不,这是自动单元格所必需的。 - Shehata Gamal
@kyo 抱歉,我刚才有点忙,可能其他人已经和你一起完成了。 - Shehata Gamal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接