0

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!

codermex
  • 3
  • 1
  • 3
  • If you don't have any UI issues, you can ignore this message. If you do, and your UI is breaking, that means that you have some constraints that cannot be satisfied all at once, and the constraints are being broken so others can be satisfied. This usually means you set up your constraints in a conflicting way, but no way to tell how unless we see your UI. – Adis Oct 14 '20 at 13:47
  • Did you ever read the error message accurately and did you ever try what the message suggests? – vadian Oct 14 '20 at 14:01
  • @vadian i'm new to swift if i did udnerstand the error i won't be asking my friend :) – codermex Oct 14 '20 at 14:04
  • @Adis i did add a photo to my UI – codermex Oct 14 '20 at 14:06

2 Answers2

1

It's incredibly difficult to solve these problems or warnings.

Here is ONE KEY TIP.

You can actually add a name to EVERY! constraint.

enter image description here

Do so.

Make the name clear and obvious like "the top constraint on the second box" or whatever.

You don't have to do every single constraint in the app, just the possibly problem ones in the relevant region.

Before anything else, you have to do that. :/

Note that there is a vast amount of discussions about this on this site, 100s of questions, eg https://stackoverflow.com/a/31833436/294884

Essentially, "your constraints have a minor problem" and specifically you likely have "one too many constraints somewhere" (or more subtly, you have to change the priority on one).

Your first step is the naming trick, to at least find out what the hell constraints Apple is referring to :/


Some suggest this BTW! http://wtfautolayout.com


For this particular question:

In this particular case, of the many, many possible problems, it turns out the OP's problem was the "two constraints" issue:

If you DO have/need two constraints, then ONE of them MUST have a DIFFERENT priority than the other.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Hi thank you very much, as i clearly mentioned in my question i don't want just how to fix i want to understand excatly what i'm doing wrong so on the next time i iknow how to deal with it, anyway i tried your approac and it seems work however i have one question. you linked to me a question and as i did change priority from (1000) to (999) the error was gone but why? it means that i decrease the priority i tell xcode i don't care about this constraint or what? – codermex Oct 14 '20 at 15:08
  • Ah, the exact answer is ***there is somewhere you have TWO constraints*** and very likely you only need one. If you DO have/need two constraints, YES, then ONE of them MUSt have a DIFFERENT priotity! – Fattie Oct 14 '20 at 15:12
  • well thank you very much now i got the idea what is going on :) thank you again – codermex Oct 14 '20 at 15:20
  • yes, it's a VERY VERY difficult issue. Quite often there is a subtle bug in iOS regarding constraints. (Here's one .. https://stackoverflow.com/q/43322973/294884 ) I personally have many questions about constraints. It's tough. – Fattie Oct 14 '20 at 15:24
0

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

CloudBalancing
  • 1,461
  • 2
  • 11
  • 22
  • Thank you very much for you answer, i have add a photo to my view and constrains as well, can you check it please and add anything that can be also helpful? – codermex Oct 14 '20 at 14:05
  • Yes, so you need to decide which constraint to remove, it seems like the system decides for you in pretty good way. I would suggest removing the width constraint on the label (width >=260). and set the label.trailing - avatar.leading to a flexible one like >=50 (where you set the minimal distance between them... See that 261 (min label width) + 51 (horizontal spacing between image and label) + 62 (image width) + 16 (margins) + 16 (margins) sums to = 402 which is more than 375 (the screen/cell width) – CloudBalancing Oct 14 '20 at 14:12
  • You can also set the priority like @Fattie suggested, it will be the simplest solution – CloudBalancing Oct 14 '20 at 15:21
  • But do note that in many cases YOU DO WANT two constraints! It is the basis of flexible layouts. It's tricky stuff. – Fattie Oct 14 '20 at 15:24