Strange UIView-Encapsulated-Layout-Height Error
Solution 1
I usually remove this warning by lowering the priority of the constraint that AutoLayout
is trying to break. So if it says:
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fd7c2ecb520 V:[UIView:0x7fd7c2ecd0e0(300)]>
Go ahead and lower that one's priority to 999
.
That should work.
Cheers.
Solution 2
if you use tableView.rowHeight = UITableViewAutomaticDimension
, you should always set estimatedRowHeight
. For example:
tableView.estimatedRowHeight = 44
or you should implement:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
otherwise you will always get the warning UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView
and the content will be collapsed in a wrong way
Solution 3
For me I use estimated height for cells, and to solve this problem, I set the priority of the constraint from label bottom to it's neighbor to 999 and it worked.
Solution 4
I've seen two cases where this happens so far. One is documented in the accepted answer, which is that your tableView:heightForRow: implementation must return the same value your constraints will compute to.
The other time this happens is if you forget to set translatesAutoresizingMaskIntoConstraints = NO (or false for Swift) on one or more autolayout views. Before adding constraints to a programmatically created view, turn off autoresizing like this:
view.translatesAutoresizingMaskIntoConstraints = false
Otherwise, if your view has a fixed width or height specified by the autoresizing mask then a constraint will be created to enforce it.
Solution 5
The following piece of code not worked for me
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Neither add translatesAutoresizingMaskIntoConstraints=false to a bunch of views.
Finally, Removing cell.updateConstraintsIfNeeded()
in datasource callback function tableView:cellForRowAt
makes Xcode content
Comments
-
Zaporozhchenko Oleksandr almost 2 years
I'm making test application, so in my tableviewCell in storyboard I have imageView & webView (webview to show html-text).
I set constraints like top/left/right/height=200 for imageView, spacing=5 between them & left/right/bot for webView, so I want to calculate my webView height programmatically and then change cell's height to stretch my webView. But I got 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 & fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSLayoutConstraint:0x7fd6f3773f90 V:[UIImageView:0x7fd6f3773e90(200)]>", "<NSLayoutConstraint:0x7fd6f3774280 UIImageView:0x7fd6f3773e90.top == UITableViewCellContentView:0x7fd6f3462710.topMargin>", "<NSLayoutConstraint:0x7fd6f3774320 V:[UIImageView:0x7fd6f3773e90]-(5)-[UIWebView:0x7fd6f3462800]>", "<NSLayoutConstraint:0x7fd6f3774370 UITableViewCellContentView:0x7fd6f3462710.bottomMargin == UIWebView:0x7fd6f3462800.bottom>", "<NSLayoutConstraint:0x7fd6f375ee40 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fd6f3462710(205)]>" )
Any suggestions?
-
Phineas Lue over 8 yearsIt works but what is the real causing under the hood?
-
CaffeineShots over 8 years@PhineasLue in my case I am having constraint(a) as default constraint then I update and add another constraint(b) where somehow hit the constraint
a
now you need choose betweena
orb
to be the final constraint that will appear in your view, if you choosea
you need to set the priority ofb
to 999 its like saying give way to constrainta
-
flatpickles over 8 yearsThanks for this! Silly that it's necessary, but I'm stoked to get rid of the bogus warning.
-
hzwzw over 8 yearsIt works. the results is showing the warning
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
have you guys meet this situation? -
Neil almost 8 yearsThis solution worked for me with Xcode Version 7.3.1 (7D1014). What I noticed is that in IB in my case, a UITableViewCell height of 131 px makes the IB auto layout engine happy, but the contentView shows as 130 px in IB which is not correct and not changeable. I think this is a bug between Xcode IB auto layout engine and iOS runtime auto layout.
-
The iCoder almost 7 yearsGetting same kind of error for a UIView for which I have set top, trailing, bottom and width constraint. When orientation changed I am modifying the top constraint constant at that time console showing same warning. Once I set priority for the top constraint to 999 , Warning goes aways. Thanks for answer it helped me a lot :). I have question but why height constraint added by the OS?
-
Trevis Thomas almost 7 yearsHeads up to anyone else reading this. There are also .estimatedSectionHeaderHeight and .estimatedSectionFooterHeight attributes as well if you are using headers and or footers.
-
Mike over 6 yearsFYI - I've seen differences in setting the estimated row height on the table view vs. implementing the function. The function works, the setter doesn't seem to in my case.
-
Ankit J over 5 yearsYes I had the same issue. Remove any setNeedsLayout and layOutIfNeeded calls when cell is being reused.
-
Ning about 5 yearsawesome! saved my day.
-
DragonCherry over 4 yearsI've applied it to a subview with fixed height on dynamic-height parent view, and it worked perfectly. It's kind of make sense.
-
trishcode about 4 yearsNice to have an explanation for why the UIView-Encapsulated-Layout-Height is 0.5 off.
-
Glenn Posadas almost 4 yearsYeah, the
tableView.separatorStyle = .none
fixed this annoying warning. -
אורי orihpt over 3 yearsBut how? where do I add it? what is the name of the constraint?