Strange UIView-Encapsulated-Layout-Height Error

29,164

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

Share:
29,164
Zaporozhchenko Oleksandr
Author by

Zaporozhchenko Oleksandr

iOS Developer

Updated on July 09, 2022

Comments

  • Zaporozhchenko Oleksandr
    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
    Phineas Lue over 8 years
    It works but what is the real causing under the hood?
  • CaffeineShots
    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 between a or b to be the final constraint that will appear in your view, if you choose a you need to set the priority of b to 999 its like saying give way to constraint a
  • flatpickles
    flatpickles over 8 years
    Thanks for this! Silly that it's necessary, but I'm stoked to get rid of the bogus warning.
  • hzwzw
    hzwzw over 8 years
    It 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
    Neil almost 8 years
    This 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
    The iCoder almost 7 years
    Getting 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
    Trevis Thomas almost 7 years
    Heads up to anyone else reading this. There are also .estimatedSectionHeaderHeight and .estimatedSectionFooterHeight attributes as well if you are using headers and or footers.
  • Mike
    Mike over 6 years
    FYI - 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
    Ankit J over 5 years
    Yes I had the same issue. Remove any setNeedsLayout and layOutIfNeeded calls when cell is being reused.
  • Ning
    Ning about 5 years
    awesome! saved my day.
  • DragonCherry
    DragonCherry over 4 years
    I'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
    trishcode about 4 years
    Nice to have an explanation for why the UIView-Encapsulated-Layout-Height is 0.5 off.
  • Glenn Posadas
    Glenn Posadas almost 4 years
    Yeah, the tableView.separatorStyle = .none fixed this annoying warning.
  • אורי orihpt
    אורי orihpt over 3 years
    But how? where do I add it? what is the name of the constraint?