iOS 8 Custom Self Sizing Table View : Unable to simultaneously satisfy constraints - ios

In UITableViewController, I am using custom UITableView cell. This cell contains a UIImage control on left side of cell and two UILabels(one is for product name and other is for product description, which can be varying length) to the right of this UIImage. I have added Auto Layout constraints on all three controls. There is no issue in Auto Layout control. However when I run the app, I get the following error messages (one for each row in the table) and a couple of top rows are not as per auto layout constraints, but however when I scroll down and then scroll up, everything is rendered as per the auto layout constraints
Debug Log:
-----------------------------------------------------------------------------
- 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x170085ff0 H:[PFImageView:0x14ee1d350(60)]>",
"<NSLayoutConstraint:0x17408a500 PFImageView:0x14ee1d350.leading == UITableViewCellContentView:0x14ee1cdf0.leadingMargin + 7>",
"<NSLayoutConstraint:0x17408a5a0 UITableViewCellContentView:0x14ee1cdf0.trailingMargin == UILabel:0x14ed08580'Naturally Sandstone Slabs'.trailing + 10>",
"<NSLayoutConstraint:0x17408a5f0 H:[PFImageView:0x14ee1d350]-(11)-[UILabel:0x14ed08580'Naturally Sandstone Slabs']>",
"<NSLayoutConstraint:0x17008c6c0 'fittingSizeHTarget' H:[UITableViewCellContentView:0x14ee1cdf0(34)]>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x170085ff0 H:[PFImageView:0x14ee1d350(60)]>
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.

The issue was because i was using a disclosure indicator in the table view. Lowering the priority of label's trailing-to-superview constraint. (I chose 990) fixed the issue.
Below is the link where i get the solution:
ios8 cell constraints break when adding disclosure indicator

Related

Why does adding constraints for all four sides cause an error?

UPDATE
Editing an XIB file, I added a button to the position that I'd like in the view and clicked the pin icon at the bottom right, and pinned all four sides of the button, with "Constrain to Margins" unchecked, as that's the exact location I would want to place the button and I wouldn't want it to move around.
But I got an error that says 'Unable to simultaneously satisfy constraints.' Why does that happen?
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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<_UILayoutSupportConstraint:0x9f494b0 V:[_UILayoutGuide:0x9f40370(20)]>",
"<_UILayoutSupportConstraint:0x9f3b6c0 V:|-(0)-[_UILayoutGuide:0x9f40370] (Names: '|':UIView:0x9f3feb0 )>",
"<_UILayoutSupportConstraint:0x9f418e0 V:[_UILayoutGuide:0x9f407c0(0)]>",
"<_UILayoutSupportConstraint:0x9f18010 _UILayoutGuide:0x9f407c0.bottom == UIView:0x9f3feb0.bottom>",
"<NSLayoutConstraint:0x9f40f10 V:[UIButton:0x9f40f40'Button']-(211)-[_UILayoutGuide:0x9f407c0]>",
"<NSLayoutConstraint:0x9f47ab0 V:[_UILayoutGuide:0x9f40370]-(539)-[UIButton:0x9f40f40'Button']>",
"<NSLayoutConstraint:0xa157b10 'UIView-Encapsulated-Layout-Height' V:[UIView:0x9f3feb0(568)]>"
)
Will attempt to recover by breaking constraint
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.
Autolayout technology work on relationship between objects and object with superview how they will draw on screen. as you mention you are trying to force object through all sides so set for specific position you can try by think how object will draw on screen by applying specific constraints.
if you want to place object of fix width and height at center of screen in all orientation then you can try
Align->Horizontal and Vertical center in container.
(Otherwise even they can crash your app if unsatisfied constraints)
Hope this will help you.
If you have a height or width assigned to the button it will conflict with the constraints you added. If you want it to always be at a certain x,y and specific size you should pin the top and left along with the width and height. If you want it to dynamically resize you should pin all 4 sides.

Adding a button in dynamic TableViewCell

I have a label of dynamic size in a TableViewCell. I have pinned it to up,down,left and right and set Lines to 0 and it is working fine. Next, I add a button below the label and and pin it to top and left. and fix the width and height. But I am getting an error at runtime
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. (Note: If you're seeing
NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the
documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fcbc302f600 UILabel:0x7fcbc3034010'Digestion of food in the...'.top == UITableViewCellContentView:0x7fcbc30334a0.topMargin>",
"<NSLayoutConstraint:0x7fcbc3037e30 UITableViewCellContentView:0x7fcbc30334a0.bottomMargin == UILabel:0x7fcbc3034010'Digestion of food in the...'.bottom + 38>",
"<NSLayoutConstraint:0x7fcbc303a920 'UIView-Encapsulated-Layout-Height' V: [UITableViewCellContentView:0x7fcbc30334a0(43.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcbc3037e30
UITableViewCellContentView:0x7fcbc30334a0.bottomMargin ==
UILabel:0x7fcbc3034010'Digestion of food in the...'.bottom + 38>
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. Here is the screenshot of my layout
Which constraint should I remove? I don't think I have added extra constraints
In interface builder check that there are no warnings (the yellow arrow on the side of the vc inspector) and check that the height of the label +the button and the margins don't exceed the cell height.
Also you said you pinned to the top, of what? The container or the label?
Use these constraint for as per your requirements:
OR
Follow these links Autolayout or Autolayout programatically
(OR)
For some reason, Xcode generates own default set of auto layout constraints on a NIB at build time. This is why I couldn't add any more manual constraints, because they were conflicting with the automatically added ones.
I resolved this the following way:
Open up the Storyboard view controller you're handling.
Select the view controller and select Editor > Resolve Auto Layout Issues > All Views in [ ] View Controller > Add Missing Constraints from the menu:
This will ensure that no additional build time constraints are created and all the constraints are now visible.-->Select all the constraints from your view controller:
This will ensure that no additional build time constraints are created and all the constraints are now visible.-->Check from the right pane the following check box: Placeholder - Remove at build time:
(This will ensure that no additional build time constraints are created and all the constraints are now visible.)
Now you can add all the auto layout constraints manually in the code.
you will have to provide some more info than that regarding the values you set in auto layout like the distance of the UILabel from top margin,down,left etc.
The problem might be that you specified the UIButton to be from top margin , it should be with respect to your UILabel as its below it so set the UIButton's vertical spacing property to the UILabel this makes sure the UIButton is placed below it.

Using auto layout and systemLayoutSizeFittingSize

I have a really simple view that I cannot for the life of me figure out the problem. I have successfully used auto layout and systemLayoutSizeFittingSize for fairly complex custom UITableViewCells. This is my first time using it for a UIView (to be used in a UITableView.tableHeaderView) and I cannot get it to work.
I have a simple label in the view that I want to cover the entire view. I have added the Trailing, Leading, Top, and Bottom constraints to the label against the parent's corresponding edges. When I do this, systemLayoutSizeFittingSize computes the correct height, but I get the following error:
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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7fbb71d9a310 V:|-(5)-[UILabel:0x7fbb71df5120'Test Label'] (Names: '|':mailapp.MailTableHeaderView:0x7fbb71d2c490 )>",
"<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>",
"<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>
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.
Now, if I remove the "bottom" constraint on the label, auto layout works correctly but now systemLayoutSizeFittingSize returns 0.
What is the right way to make the constraints satisfy the auto layout engine but also to have systemLayoutSizeFittingSize compute the correct size?
As I mentioned, the most baffling thing is that I have used this successfully for complex auto layouts, but I noticed all those were UITableViewCells. I'm not sure if there is a difference.
I figured out the problem, but I don't understand why. For the UITableViewHeader, I cannot do the typical auto layout thing for the view. That is, I have to set translatesAutoresizingMaskIntoConstraints to be true. Effectively, I cannot use auto layout for the header view, and I have to set the frame manually.
Within the header view, I can use auto layout for the sub views successfully.
So my algorithm goes like this for constructing the tableHeaderView:
Create the header view with any arbitrary frame
Compute the constraints (of the header view's subviews)
Set the frame.width of the header view to match the parent tableView.frame.width
Layout the header view
Use systemLayoutSizeFittingSize to compute the height
Update the header view's frame.height
This works for me. I was not able to get it to work if I created the header view with translatesAutoresizingMaskIntoConstraints set to false.
As per the Apple Visual Format Language the following appears to be setting a '0' height constraint on your HeaderView.
E.g.
"<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
I would start by investigating this, as having a height of 0 will likely mess up your other label constraints for top and bottom spacing.

Auto layout unable to satisfy constraints when activating a segue

I have a custom table view cell with constraints suggested by Auto Layout. In the storyboard all the constraints are marked with blue lines indicating that they have no errors. The tableview launches fine and the layout works but as soon as I hit the cell to activate a segue I get this:
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.
I have not added any code that adds or removes constraints to my layout. All the constraints were auto-added by Auto layout.
The error only shows up when I hit the cell which activates a segue to take the user to the next view. When I hit the back button, the tableview returns but without any constraints.
The segue is set as Show(e.g. Push) in the Attributes Inspector.
Here's the rest of the message:
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7ff1c276fec0 UIImageView:0x7ff1c2496b40.top == UITableViewCellContentView:0x7ff1c2496630.topMargin + 15>",
"<NSLayoutConstraint:0x7ff1c276ff10 UIImageView:0x7ff1c2496b40.centerY == UITableViewCellContentView:0x7ff1c2496630.centerY>",
"<NSLayoutConstraint:0x7ff1c24c9a70 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff1c2496630(43.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7ff1c276ff10 UIImageView:0x7ff1c2496b40.centerY == UITableViewCellContentView:0x7ff1c2496630.centerY>
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.
2015-01-06 11:35:58.645 FitLift[4174:130902] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7ff1c2494c00 UIImageView:0x7ff1c277c070.top == UITableViewCellContentView:0x7ff1c277bfa0.topMargin + 15>",
"<NSLayoutConstraint:0x7ff1c24a3360 UIImageView:0x7ff1c277c070.centerY == UITableViewCellContentView:0x7ff1c277bfa0.centerY>",
"<NSLayoutConstraint:0x7ff1c24b7010 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff1c277bfa0(43.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7ff1c24a3360 UIImageView:0x7ff1c277c070.centerY == UITableViewCellContentView:0x7ff1c277bfa0.centerY>
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.
2015-01-06 11:35:58.646 FitLift[4174:130902] 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7ff1c24c4390 UIImageView:0x7ff1c24bc880.top == UITableViewCellContentView:0x7ff1c24bc3d0.topMargin + 15>",
"<NSLayoutConstraint:0x7ff1c24c43e0 UIImageView:0x7ff1c24bc880.centerY == UITableViewCellContentView:0x7ff1c24bc3d0.centerY>",
"<NSLayoutConstraint:0x7ff1c24c7880 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7ff1c24bc3d0(43.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7ff1c24c43e0 UIImageView:0x7ff1c24bc880.centerY == UITableViewCellContentView:0x7ff1c24bc3d0.centerY>
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.
From the error log, it seems the two conflicting constraints are the image view's top constraint and centerY constraint. Choose only one--iOS is confused what value to assign to the image view's y-coordinate. Do you want the image view to be a certain distance from the top, or to be a certain distance from the superview's vertical center?
As a general rule, if you want your UI to look the way you want to, DO NOT let Xcode auto-add the constraints for you. Define it yourself. We can't really help you here because we don't know the other constraints that Xcode added but don't conflict.
Try lowering one of your vertical constraint priorities to 999 or less.
"<NSLayoutConstraint:0x174091d00 V:|-(10)-[UILabel:0x14562e580'This'] (Names: '|':UITableViewCellContentView:0x17418d5b0 )>",
"<NSLayoutConstraint:0x174091e40 UITableViewCellContentView:0x17418d5b0.bottomMargin >= UILabel:0x14562e580'This'.bottom + 200>",
"<NSLayoutConstraint:0x17408c940 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x17418d5b0(43.5)]>"
I was getting this, notice the last one, encapsulated height 43.5, I'm guessing its a default height, and if your vertical constraints don't fit within that 43.5, the error is thrown. I'm still uncertain how the self sizing cells work, but lowering priorities silenced the warning. I tested a cell shorter than the 43.5 and it also silenced it.
I had a similar problem and it was fixed by setting the tableview's estimatedRowHeight. The default 43.5 wasn't enough to even cover the fixed number of points I set (e.g., my top label in the cell was 10 points from the top of the cell, the bottom label was 10 points from the bottom, and space between rows of labels, etc.)

Project really has no decent constraints yet, why is it giving me this constraint warning every time?

Every time I run my project, I get this warning in the console:
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. (Note: If you're seeing
NSAutoresizingMaskLayoutConstraints that you don't understand, refer
to the documentation for the UIView property
translatesAutoresizingMaskIntoConstraints) (
"<_UIScrollViewAutomaticContentSizeConstraint:0x8cc6830 UITableView:0xb3f5c00.contentHeight{id: 112} == -7.000000>" )
Will attempt to recover by breaking constraint
<_UIScrollViewAutomaticContentSizeConstraint:0x8cc6830
UITableView:0xb3f5c00.contentHeight{id: 112} == -7.000000>
Break on objc_exception_throw to catch this in the debugger. The
methods in the UIConstraintBasedLayoutDebugging category on UIView
listed in may also be helpful.
It's from a UITableView, and I'm definitely not touching the UIScrollView directly. It only has a label in each cell with these constraints:
I Googled and Googled but couldn't find anything. Does anyone know why it's complaining about UIScrollView?
if you want to remove the warnings, the quickest way to do it is in the storyboard.
select the view controller that causes the warnings to be displayed and select: clear all constrains in yourViewControllersName.
another option will be to select: reset to suggested constrains in yourViewControllersName.
these action will either clear all constraints, or add what ever Xcode thinks are the most suitable constraints to the view controller.
resting to suggested constraints will sometimes (or is it always?) add constraints that will not fit your needs..

Resources