"Unable to simultaneously satisfy constraints" with UITableViewCell - ios

I used the Interface Builder to create a pretty simple layout inside a UITableViewCell for my Xcode 13.0/Swift 5/iOS 13.5 app:
UIStackView (with 2 UIViews and UILabels inside) at the top and a UIView (with a UIImageView inside) below it.
The exact constraints are currently:
UIStackView: 10pts leading, 10pts trailing, 0pts top, 10pts to the UIView at the bottom, height of 50pts
UIView: 20pts leading, 20pts trailing, 10pts bottom, (10pts top from the UIStackView), height=width
UIImageView inside the UIView: 0pts leading/trailing/top/bottom, align center x to Superview (=UIView)
Contrary to other questions about the warning here on stackoverflow I don't set the UITableViewCell's height directly but want it to be set by its content. The UIImageView's image is set at runtime and should keep its shape, so square images should be square and the other rectangular images should be centered horizontally (-> invisible bars left/right, if necessary) but not vertically.
Xcode doesn't complain about missing constraints but this layout keeps giving me the infamous "Unable to simultaneously satisfy constraints" warning.
[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:0x600002d22d50 UIStackView:0x7fe4fa293330.height == 50 (active)>",
"<NSLayoutConstraint:0x600002d22df0 UIView:0x7fe4fa29d1c0.height == UIView:0x7fe4fa29d1c0.width (active)>",
"<NSLayoutConstraint:0x600002d23070 V:|-(0)-[UIStackView:0x7fe4fa293330] (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d23110 H:[UIView:0x7fe4fa29d1c0]-(20)-| (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d23160 V:[UIStackView:0x7fe4fa293330]-(10)-[UIView:0x7fe4fa29d1c0] (active)>",
"<NSLayoutConstraint:0x600002d231b0 H:|-(20)-[UIView:0x7fe4fa29d1c0] (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d23200 V:[UIView:0x7fe4fa29d1c0]-(10)-| (active, names: '|':UITableViewCellContentView:0x7fe4fa2840f0 )>",
"<NSLayoutConstraint:0x600002d36530 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe4fa2840f0.height == 325.5 (active)>",
"<NSLayoutConstraint:0x600002d364e0 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7fe4fa2840f0.width == 295 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002d22df0 UIView:0x7fe4fa29d1c0.height == UIView:0x7fe4fa29d1c0.width (active)>
I already checked with wtfautolayout.com but the only thing wrong that I can see is the height: If the width is 295, then the height should be exactly 50+10+295-40+10=325 (UIStackView.height + constraint in-between + UIView.width - leading/trailing constraints + bottom constraint), not 325.5. Everything else looks fine to me, so I'm not sure why it would calculate that wrong height (and if that's even the problem).
I already tried a couple of things:
Added the UIStackView's fixed height (didn't use it before everything looked fine and it didn't complain about it, still got the warning though)
UIView.width = UIImageView.height (1:1 aspect ratio, instead of the one above)
UIImageView.width = UIImageView.height (1:1 aspect ratio, instead of the one above)
Removed the "center x" constraint
Nothing worked, I keep getting the constraint warning and if it doesn't show up in portrait mode because of something I changed, then it does as soon as I switch to landscape mode. How do I fix this in IB/without code?

Try changing the priority of the height=width constraint to High instead of Required

Related

Fixed cell width - unable to simultaneously satisfy constraints

Just making my first steps in IOS development, and I want to bring my Android app to IOS.
The issue it with UICollectionView. I need the cells to be of fixed constant width, and the image inside the cell - of fixed height. So, I've set the width constraint of StackView to 105 and height constraint for ImageView to 147.
Everything looks as expected, however I get warnings in the console:
2021-02-21 22:23:17.490472+0200 myapp-ios[20727:783655] [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:0x600002193890 UIStackView:0x7f90efc173d0.width == 105 (active)>",
"<NSLayoutConstraint:0x6000021938e0 H:[UIStackView:0x7f90efc173d0]-(0)-| (active, names: '|':UIView:0x7f90efc1d840 )>",
"<NSLayoutConstraint:0x600002193930 H:|-(0)-[UIStackView:0x7f90efc173d0] (active, names: '|':UIView:0x7f90efc1d840 )>",
"<NSLayoutConstraint:0x600002193a20 'UIIBSystemGenerated' myapp_ios.ShowCell:0x7f90efc23ec0.leading == UIView:0x7f90efc1d840.leading (active)>",
"<NSLayoutConstraint:0x600002193a70 'UIIBSystemGenerated' H:[UIView:0x7f90efc1d840]-(0)-| (active, names: '|':myapp_ios.ShowCell:0x7f90efc23ec0 )>",
"<NSLayoutConstraint:0x6000021967b0 'UIView-Encapsulated-Layout-Width' myapp_ios.ShowCell:0x7f90efc23ec0.width == 50 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600002193890 UIStackView:0x7f90efc173d0.width == 105 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
I also tried to set width and height constraints of ImageView only to 105x147, it looks the same, and the warning is pretty much the same. Basically if no width or height constraints set - there is no warning, once I add any of them - it appears.
Here are the constraints:
I would appreciate you helping me understand the potential problem.
I think removing the width constraint of the stack view will work.

Dynamic Height Table Cell with Autolayout and UITableAutomaticDimension (Dynamic UILabel + several UIImageViews)

I looked through the articles related with dynamic table cell and auto layout for 3 days and not working so far.
Below is the table cell what I wanted. Here the main issue is UILabel for post text and UIImages.
Here is the hierarchy of the UI elements.
- Content View
+ ...
+ UILabel for text - dynamic height
+ UIView - image view container
* UIImageView
* UIImageView
* ....
Label has line break mode wrap text and lines set to 0.
Label and the container view has constraints for top, bottom, leading and trailing. ImageViews are added at runtime and has constraint for top, leading, trailing, bottom and height constraints.
First image view has top constraint to container view and last image view has bottom constraint to container view and others has top constraint to upper image view.
When the table is first loaded (cells has different image counts), it looks fine, but when I scroll up and down, constraints are breaking in some cells and images are overlapping inside cells.
Here is the error output:
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:0x17428aaf0 V:|-(0)-[UIImageView:0x14be77ed0] (active, names: '|':UIView:0x14be75b20 )>",
"<NSLayoutConstraint:0x17428a6e0 UIImageView:0x14be77ed0.height == 160 (active)>",
"<NSLayoutConstraint:0x17428acd0 UIImageView:0x14be77ed0.bottom == UIView:0x14be75b20.bottom (active)>",
"<NSLayoutConstraint:0x174289650 V:|-(0)-[UIImageView:0x14be43ce0] (active, names: '|':UIView:0x14be75b20 )>",
"<NSLayoutConstraint:0x17428bb80 UIImageView:0x14be43ce0.height == 160 (active)>",
"<NSLayoutConstraint:0x17428be50 V:[UIImageView:0x14be43ce0]-(10)-[UIImageView:0x14be74b10] (active)>",
"<NSLayoutConstraint:0x17428bfe0 UIImageView:0x14be74b10.height == 160 (active)>",
"<NSLayoutConstraint:0x17428c080 UIImageView:0x14be74b10.bottom == UIView:0x14be75b20.bottom (active)>"
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x17428be50 V:[UIImageView:0x14be43ce0]-(10)-[UIImageView:0x14be74b10] (active)>
Please help me with this issue. Thank you.
To make sure the stack view does not contain old images when the cell is dequeued, you need to clean it in prepareForReuse():
override func prepareForReuse() {
super.prepareForReuse()
stack.arrangedSubviews.forEach {
stack.removeArrangedSubview($0)
$0.removeFromSuperview()
}
}
I would suggest not to give your images height, instead add a vertical stackview into your container view and add all your images in there. make sure they have content mode set to aspect fit and the stackview should take care of the rest. don't forget to constrain the stackview to the bottom of the label and the bottom of the cell, so it knows how much space it has. it will take care of your images by itself
ImageViews are added at runtime and has constraint for top, leading, trailing, bottom and height constraints.
This is your error message. Remove either height or one of the constraint that you choose either bottom or top contraint. You can not have all 4 sides constrained and add a height constrant to that.
Sidenote:
That goes same for width, if you set a width constraint, you can not have both sides leading/trailing constrained at the same time.
EDIT:
Also, easier off with using a UIStackView as suggested in the comments sections.

Size Classes and Stack Views Axis constraints conflicts

I'm trying something really simple and I do not understand the constraint conflict resulting. Let me explain:
I have a nice view controller with a full screen vertical UIStackView (contraints set to 0 to top, leading, trailing and bottom) with two views inside.
The top view as a constrained height of 200pt, the bottom view has no constraint. As the stack view has a Fill distribution mode, everything is fine.
Now, I want the stack view to switch to horizontal axis on landscape on iPhones. So, on my storyboard I select the wAny|hCompact size class, uninstall the height constraint, add a width constraint, and select horizontal axis...
The behavior works alright, unfortunately, it detects a constraint conflict I can't explain :
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:0x7fba62c1b7f0 H:[UIStackView:0x7fba62c0a3c0]-(0)-| (Names: '|':UIView:0x7fba62c0bd10 )>",
"<NSLayoutConstraint:0x7fba62c1b890 H:|-(0)-[UIStackView:0x7fba62c0a3c0] (Names: '|':UIView:0x7fba62c0bd10 )>",
"<NSLayoutConstraint:0x7fba62c1ba80 H:[UIView:0x7fba62c15420(200)]>",
"<NSLayoutConstraint:0x7fba62d1c230 'UISV-canvas-connection' UIStackView:0x7fba62c0a3c0.leading == UIView:0x7fba62c15420.leading>",
"<NSLayoutConstraint:0x7fba62dcad80 'UISV-canvas-connection' H:[UIView:0x7fba62c15420]-(0)-| (Names: '|':UIStackView:0x7fba62c0a3c0 )>",
"<NSLayoutConstraint:0x7fba62dc69d0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fba62c0bd10(736)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fba62c1ba80 H:[UIView:0x7fba62c15420(200)]>
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.
So if someone can tell me what is wrong!
Ran into this problem and it appears to possibly be a bug in UIStackView where an internally generated constraint from the previous/default size class is lingering. This workaround (Swift) works for me:
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
self.setNeedsLayout()
self.layoutIfNeeded()
}

Creating a "self-sizing" UITableViewHeaderView

OK, a really simple version of this that breaks...
Create a UIView in a nib.
Add an imageView to it. Give the image view a constraint for an Aspect Ratio of 1:1.
Now add constraints from the image view to the edge of the view. (0 size to pin it to the edges).
Now add the view as a UITableView tableHeaderView.
When you run the application on different devices it will break the constraints and remove the aspect ratio constraint. I don't want this, I want it to change the height of the view to be equal to the width.
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:0x7fad01efbbe0 UIImageView:0x7fad047209b0.width == UIImageView:0x7fad047209b0.height>",
"<NSLayoutConstraint:0x7fad04703630 H:[UIImageView:0x7fad047209b0]-(0)-| (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad04703680 V:|-(0)-[UIImageView:0x7fad047209b0] (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad04703740 V:[UIImageView:0x7fad047209b0]-(0)-| (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad04703790 H:|-(0)-[UIImageView:0x7fad047209b0] (Names: '|':VenueHeaderView:0x7fad047204c0 )>",
"<NSLayoutConstraint:0x7fad0429bcf0 'UIView-Encapsulated-Layout-Height' V:[VenueHeaderView:0x7fad047204c0(300)]>",
"<NSLayoutConstraint:0x7fad0429bca0 'UIView-Encapsulated-Layout-Width' H:[VenueHeaderView:0x7fad047204c0(375)]>" )
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fad01efbbe0 UIImageView:0x7fad047209b0.width == UIImageView:0x7fad047209b0.height>
It seems that there are constraints UIView-Encapsulated-Layout-Height being added to the superview. But these are added with the wrong value. It should recalculate these based on the internal constraints of the header view.

UITableViewAutomaticDimension not working properly in iOS 9

In my app I have custom tableViewCells with a fixed ratio (16x9). To achieve that, I placed a view in the cell, fixed it to its parent view (although I did it in interface builder: V/H:|-[innerView]-|).
Also, I put a ratio constraint on it.
In my tableViewController I'm using UITableViewAutomaticDimension as a table cell height.
The estimated row height is 180, wich is the exact size the cell will have on a 320px wide display (as I, as you can see, do).
I'm still deploying for 8.4, but when running the project on an Device with iOS 9, I'm getting tons of auto layout warnings, although everything works fine and looks perfect.
The warning itself is absolutely right. There are two constraints I don't want – these that iOS added on its own.
2015-09-29 11:24:57.771 app[1039:324736] 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:0x147d3e0d0 UIView:0x14901ff70.height == 0.5625*UIView:0x14901ff70.width>",
"<NSLayoutConstraint:0x147dd0210 H:|-(0)-[UIView:0x14901ff70] (Names: '|':UITableViewCellContentView:0x14901f960 )>",
"<NSLayoutConstraint:0x147deeca0 V:|-(0)-[UIView:0x14901ff70] (Names: '|':UITableViewCellContentView:0x14901f960 )>",
"<NSLayoutConstraint:0x149053c30 V:[UIView:0x14901ff70]-(0)-| (Names: '|':UITableViewCellContentView:0x14901f960 )>",
"<NSLayoutConstraint:0x147dbc2b0 H:[UIView:0x14901ff70]-(0)-| (Names: '|':UITableViewCellContentView:0x14901f960 )>",
"<NSLayoutConstraint:0x149070800 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14901f960(179.5)]>",
"<NSLayoutConstraint:0x1490707b0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x14901f960(320)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x147d3e0d0 UIView:0x14901ff70.height == 0.5625*UIView:0x14901ff70.width>
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 only thing I see here are the missing 0.5 pixels that iOS subtracted somehow magically.
The issue is the two constraints that the tableview adds automatically. UIView-Encapsulated-Layout-Height and width are probably the height and width the table view calculated for the cell during the initial load, based on the cell's constraints at that time.
"<NSLayoutConstraint:0x149070800 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x14901f960(179.5)]>",
"<NSLayoutConstraint:0x1490707b0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x14901f960(320)]>"
As the priorities for these constraints are 1000 what you can do is lower your height and width constraint priorities and let the encapsulated size to be applied when needed (at load).
Seems you have added two ratio constraints added on two different views.
One that should be there is,
<NSLayoutConstraint:0x14d939e00 UIView:0x14da107f0.width ==
1.77778*UIView:0x14da107f0.height>
has memory address 0x14d939e00 added on UIView with address 0x14da107f0.
The other one is breaking.
<NSLayoutConstraint:0x14c5eae90 UIView:0x14c5ead30.height ==
0.5625*UIView:0x14c5ead30.width>
This one is added on a UIView (0x14c5ead30). Look for this view and remove this ratio constraint.

Resources