简体   繁体   English

LayoutConstraints - 无法同时满足约束

[英]LayoutConstraints - Unable to simultaneously satisfy constraints

I have an app, when I click on my profile page, I kept getting these errors in my console 我有一个应用程序,当我点击我的个人资料页面时,我一直在我的控制台中收到这些错误

I have tried 我试过了

在此输入图像描述

This is the entire constraint that I have on my profile page 这是我在个人资料页面上的整个约束

在此输入图像描述

I kept getting 我不断得到

在此输入图像描述

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.

This is what I have 这就是我所拥有的

//
//  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()
    }

} 

I just want to get rid of the error message. 我只是想摆脱错误信息。

Update 更新

@Sandeep, after trying your answer, I ended up with this final constraint @Sandeep,在尝试你的答案后,我最终得到了这个最后的约束

在此输入图像描述

This is the result 这是结果

在此输入图像描述

Can you please suggest a bit more ? 你能再多说一点吗?

Step 1: 步骤1:

Drag a View to your cell and apply horizontally and vertically center constraints to to your view (This will show few constraint breaks but hang on a bit) 将视图拖动到您的单元格并将水平和垂直中心约束应用到您的视图(这将显示一些约束中断,但稍微挂起)

在此输入图像描述

Step 2: 第2步:

Drag imageView to view and apply height constraint/ aspect ratio/ leading / height and bottom constraint as shown 拖动imageView以查看并应用高度约束/纵横比/前导/高度和下限约束,如图所示

在此输入图像描述

Step 3: 第3步:

Drag UILabel to View preferably right side of ImageView and apply trailing constraint and leading constraint to label. 将UILabel拖动到View最好是在ImageView的右侧,并将尾随约束和前导约束应用于标签。 Leading constraint will be to content view of cell while leading constraint to UIImageView. 在向UIImageView引出约束的同时,前导约束将是单元格的内容视图。

Trailing Constraint to Label 跟踪约束到标签

在此输入图像描述

Now add Leading constraint to UIImageView (Control drag from label to imageView and select horizontal spacing) 现在将Lead约束添加到UIImageView(控制从标签拖动到imageView并选择水平间距)

在此输入图像描述

Finally control drag from label to imageView again and select center vertically 最后再次控制从标签拖动到imageView并垂直选择中心

在此输入图像描述

That should be it :) 那应该是:)

If everything is fine final constraints should look like 如果一切都很好,最终约束应该是这样的

在此输入图像描述

Hope this helps 希望这可以帮助

What have you did in your code seems to be fine, but it has nothing to do with what have you seen in the console. 您在代码中做了什么似乎没问题,但它与您在控制台中看到的内容无关。

First, you should know that red constraints means that there is something wrong, more specifically it means that the interface builder is unable to recognize what to achieve because there is at least one constraint missed (and that's why you are seeing these complains on the console). 首先,您应该知道红色约束意味着存在错误,更具体地说,这意味着界面构建器无法识别要实现的内容,因为至少有一个约束丢失(这就是您在控制台上看到这些抱怨的原因)。

Therefore, I would highly recommend to avoid following the approach of "Adding Missing Constraints" or "Reset to Suggested Constraints" , it is not an elegant option to deal with constraints; 因此,我强烈建议避免遵循“添加缺失约束”“重置为建议约束”的方法 ,这不是处理约束的优雅选项; Most of the time, even if they didn't add red (missing) constraints, it won't leads to the expected result, which seems to be logical because it can't be so magical to read your mind thoughts and translate it, they could be more than just one aspect for determining the way of how to setup the constraints! 大多数时候,即使他们没有添加红色(缺失)约束,它也不会导致预期的结果,这似乎是合乎逻辑的,因为阅读你的思想思想并翻译它不是那么神奇,它们可能不仅仅是确定如何设置约束的方式的一个方面! Instead, do it by your self, that's the good approach for achieving what's exactly it should look/behave. 相反,由你自己来做,这是实现它应该看起来/表现的好方法。 Keep in mind that it is not just one static way to do it, after getting understanding the that Auto Layout works you figure that there is more than one way to achieve it. 请记住,这不仅仅是一种静态的方法,在了解了自动布局工作之后,您会发现有多种方法可以实现它。

In general, the expected result is to see blue lines (constraints) instead of red or orange. 通常,预期的结果是看到蓝线(约束)而不是红色或橙色。

Now, because it might be broad to describe how to setup your constraints (Auto Layout), I would suggest to check: Understanding Auto Layout from Apple, it is a very good place to start with. 现在,因为描述如何设置约束(自动布局)可能很广泛,我建议检查: 了解 Apple的自动布局 ,这是一个非常好的开始。

1- Make sure that only one path of elements from top to bottom is hooked to the contentView , if you see red lines , disconnect bottom constraint and change middle items constraints , then re-hook it again 1-确保只有一个从上到下的元素路径连接到contentView,如果看到红线,断开底部约束并更改中间项约束,则再次重新挂钩

2- Set bottom most constraint priority to 999 2-将最底部约束优先级设置为999

3- Set leading to the imageView and only the bottom - most element should be hooked to the contentView's bottom 3-设置通向imageView,只有底部 - 大多数元素应该连接到contentView的底部

// //

Blue lines here should only be constraints with img width&height 这里的蓝线应该只是img宽度和高度的约束 在此输入图像描述

// //

Set this in viewDidLoad 在viewDidLoad中设置它

tableView.estimatedRowHeight = 200;

tableView.rowHeight = UITableViewAutomaticDimension;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM