Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 271
Julian F. Weinert Avatar asked Sep 21 '15 10:09

Julian F. Weinert


2 Answers

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).

like image 196
Istvan Avatar answered Oct 20 '22 08:10

Istvan


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.

like image 1
BangOperator Avatar answered Oct 20 '22 08:10

BangOperator