首页 > 解决方案 > Unable to simultaneously satisfy constraints. Probably at least one of the constraints

问题描述

I'm doing an ios chat application using swift 5 and storyboard, one of my views contain a table view and cell however i'm getting this kind of error i didn't udnerstand what is it exacctly and how can i fix it!

Apparently whenever the tableView.reload() function is called this problem happens.

Here it is my view

view storyboard tableview

As you can see in the image there is a tableview, and within the tableCell, there is a view that contain label to display username, and in the right an image view to display user photo

2020-10-14 14:59:17.638363+0200 Chatiw[17620:455807] [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:0x6000000e7840 UILabel:0x7fafbe739be0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000e7930 UIImageView:0x7fafbe73a4d0.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000e7b10 UILabel:0x7fafbe739be0.leading >= UITableViewCellContentView:0x7fafbe739850.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000e7c50 H:[UILabel:0x7fafbe739be0]-(51)-[UIImageView:0x7fafbe73a4d0]   (active)>",
    "<NSLayoutConstraint:0x6000000e7ca0 UIImageView:0x7fafbe73a4d0.trailing == UITableViewCellContentView:0x7fafbe739850.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x600000096580 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafbe739850.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000e7e80 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9cb60'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe739850 )>",
    "<NSLayoutConstraint:0x6000000e7f20 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9cb60'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe739850 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000e7840 UILabel:0x7fafbe739be0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.642314+0200 Chatiw[17620:455807] [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:0x600000096710 UILabel:0x7fafbe43e0f0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000968f0 UIImageView:0x7fafbe43e360.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000ea620 UILabel:0x7fafbe43e0f0.leading >= UITableViewCellContentView:0x7fafbe43dd60.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000eb8e0 H:[UILabel:0x7fafbe43e0f0]-(51)-[UIImageView:0x7fafbe43e360]   (active)>",
    "<NSLayoutConstraint:0x6000000eb7f0 UIImageView:0x7fafbe43e360.trailing == UITableViewCellContentView:0x7fafbe43dd60.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x600000097250 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafbe43dd60.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000eb9d0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9a5a0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe43dd60 )>",
    "<NSLayoutConstraint:0x6000000eac60 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9a5a0'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe43dd60 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000096710 UILabel:0x7fafbe43e0f0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.644909+0200 Chatiw[17620:455807] [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:0x6000000eb930 UILabel:0x7fafc084b7c0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000ea8f0 UIImageView:0x7fafc084bb60.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000eab20 UILabel:0x7fafc084b7c0.leading >= UITableViewCellContentView:0x7fafc084b630.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000ea8a0 H:[UILabel:0x7fafc084b7c0]-(51)-[UIImageView:0x7fafc084bb60]   (active)>",
    "<NSLayoutConstraint:0x6000000ea990 UIImageView:0x7fafc084bb60.trailing == UITableViewCellContentView:0x7fafc084b630.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000cf700 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafc084b630.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000d3250 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9a760'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc084b630 )>",
    "<NSLayoutConstraint:0x6000000d2df0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9a760'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc084b630 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000eb930 UILabel:0x7fafc084b7c0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.647864+0200 Chatiw[17620:455807] [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:0x6000000efb60 UILabel:0x7fafc0d0a3d0.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000d2da0 UIImageView:0x7fafc084c430.width == 62   (active)>",
    "<NSLayoutConstraint:0x6000000d3b60 UILabel:0x7fafc0d0a3d0.leading >= UITableViewCellContentView:0x7fafc0d09aa0.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000d3840 H:[UILabel:0x7fafc0d0a3d0]-(51)-[UIImageView:0x7fafc084c430]   (active)>",
    "<NSLayoutConstraint:0x6000000d37a0 UIImageView:0x7fafc084c430.trailing == UITableViewCellContentView:0x7fafc0d09aa0.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x6000000eac10 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafc0d09aa0.width == 375   (active)>",
    "<NSLayoutConstraint:0x6000000d05f0 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001a9a840'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc0d09aa0 )>",
    "<NSLayoutConstraint:0x6000000d3f20 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001a9a840'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafc0d09aa0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000efb60 UILabel:0x7fafc0d0a3d0.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-10-14 14:59:17.650367+0200 Chatiw[17620:455807] [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:0x6000000978e0 UILabel:0x7fafbe442740.width >= 261   (active)>",
    "<NSLayoutConstraint:0x6000000979d0 UIImageView:0x7fafbe4429b0.width == 62   (active)>",
    "<NSLayoutConstraint:0x600000097bb0 UILabel:0x7fafbe442740.leading >= UITableViewCellContentView:0x7fafbe4425b0.leadingMargin   (active)>",
    "<NSLayoutConstraint:0x600000097cf0 H:[UILabel:0x7fafbe442740]-(51)-[UIImageView:0x7fafbe4429b0]   (active)>",
    "<NSLayoutConstraint:0x600000097d40 UIImageView:0x7fafbe4429b0.trailing == UITableViewCellContentView:0x7fafbe4425b0.trailingMargin   (active)>",
    "<NSLayoutConstraint:0x60000009c780 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fafbe4425b0.width == 375   (active)>",
    "<NSLayoutConstraint:0x600000097f20 'UIView-leftMargin-guide-constraint' H:|-(16)-[UILayoutGuide:0x600001ade840'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe4425b0 )>",
    "<NSLayoutConstraint:0x6000000efc00 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600001ade840'UIViewLayoutMarginsGuide']-(16)-|(LTR)   (active, names: '|':UITableViewCellContentView:0x7fafbe4425b0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000000978e0 UILabel:0x7fafbe442740.width >= 261   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

Anyone please can explain to me exactly what is this problem so i can understand it and how can i fix it? i already have this problem on all my table views!

标签: iosswiftstoryboardswift5

解决方案


It is a bit of hard to answer without a UI example (both the ViewController and the Cell). But it seems that the TableViewCell subviews constraints are not complying with the actual width of the cell.

You set a bigger width constraints to the label and image view distances and sizes than there is space in the screen.

An example that might work for you: [-(leading=0)-(label)-(<=50)-(imageView=60)-(trailing=0)-]

This way there will be a minimum of 50 points constraints between the imageView and the label...

Several Suggestions:

  1. No need to set a width constrain on the label - it should be infer from the actual text in it, just describe (with constraints) the distance to other views.

  2. No need for setting a width/height constraints on the imageView at all and see the contentMode property for how to adapt the image in it.

  3. Unless you want to centre the subviews within the cell, no need to set leading/trailing constraints as >= / <= just pin them to the super view (the cell in your case)

Another tip is if the label might contain a long string is to set its numberOfLines property to 0. And set self.tableView.rowHeight = UITableViewAutomaticDimension

See this awesome explanation about TableViewCell constraints


推荐阅读