LayoutConstraints-无法同时满足约束

时间:2018-07-31 16:15:03

标签: ios swift xcode nslayoutconstraint

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

我尝试过

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

您能请点建议吗?

3 个答案:

答案 0 :(得分:2)

第1步:

将一个View拖到您的单元格上,并将水平和垂直居中的约束应用于您的视图(这将显示很少的约束中断,但会稍等)

enter image description here

第2步:

拖动imageView以查看并应用高度限制/长宽比/前导/高度和底部限制

enter image description here

第3步:

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

跟踪约束到标签

enter image description here

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

enter image description here

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

enter image description here

应该是:)

如果一切都很好,最终约束应该看起来像

enter image description here

希望这会有所帮助

答案 1 :(得分:1)

您在代码中所做的事情似乎还不错,但这与您在控制台中看到的内容无关。

首先,您应该知道红色约束表示有问题,更具体地说,这意味着接口构建器无法识别要实现的目标,因为至少缺少一个约束(这就是为什么您会看到这些抱怨的原因在控制台上)。

因此,我强烈建议避免使用“添加缺少的约束” “重置为建议的约束” 的方法, 不是是处理约束的一种很好的选择;在大多数情况下,即使他们没有添加红色(缺失)约束,也不会导致预期的结果,这似乎是合乎逻辑的,因为阅读您的思想并翻译它并没有那么神奇,它们可能不仅仅是确定如何设置约束的方式的一个方面!取而代之的是,自己动手做,这是实现其实际外观/行为的好方法。请记住,这不仅是一种静态的方法,在了解了自动版式的工作原理之后,您会发现实现这一目标的方法不止一种。

通常,预期结果是看到蓝线(约束),而不是红色或橙色。

现在,因为可能会广泛地描述如何设置约束(自动布局),所以我建议检查一下:Understanding Auto Layout(来自Apple),这是一个很好的起点。

答案 2 :(得分:0)

1-确保从上到下仅将元素的一条路径连接到contentView上,如果看到红线,请断开底部约束并更改中间项约束,然后再次将其重新连接

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

3-设置通往imageView的位置,并且仅将其底部放置-大多数元素应钩在contentView的底部

//

这里的蓝线应仅是具有img宽度和高度的约束 enter image description here

//

在viewDidLoad中设置

tableView.estimatedRowHeight = 200;

tableView.rowHeight = UITableViewAutomaticDimension;