UITableViewAutomaticDimension not working properly in iOS 9 - ios

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.

Related

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()
}

Contraints greyed-out in Xcode

I am trying to debug+understand autolayout constraints and I notice that when debugging the view with xcode (using the cool layer thing) I noticed that on one element in the view the constraints look like this
and the view is indeed ignoring these constraints.
All constraints have the same priority (1000) since I want them all. All constraints were made with interface builder and not through code, and there are not warning or conflicts in IB.
But in runtime I do see 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x79684f10 V:[UIImageView:0x79686800(>=160)]>",
"<NSLayoutConstraint:0x7968a310 V:[UIImageView:0x79686800]-(130.5)-| (Names: '|':UIView:0x79686790 )>",
"<NSLayoutConstraint:0x7968a340 V:|-(0)-[UIImageView:0x79686800] (Names: '|':UIView:0x79686790 )>",
"<NSLayoutConstraint:0x796997b0 'UIView-Encapsulated-Layout-Height' V:[CoverCell:0x79686570(192)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7969cd30 h=-&- v=-&- UIView:0x79686790.height == CoverCell:0x79686570.height>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x79684f10 V:[UIImageView:0x79686800(>=160)]>
So from this I understand that some rules conflict, but I'm not sure how to read this
the >=160 is a rule on the UIImageView so it would have height of atleast 160 and 130.5 is the bottom padding of the imageview (so when using systemLayoutSizeFittingSize:UILayoutFittingCompressedSize the height won't be 0. So the minimum height for the entire cell is 160+130.5)
The rest of the error I don't understand.
What is wrong with the constraints and why do constraint conflicts occur in runtime and not in IB?
XCode 6 now supports different layouts. This greyed out constraints exist in Compact Width | Any Height layout, for example, but you currently editing Any Width | Any Height.
More detailed:
Storyboard View Elements Greyed Out
It depends. If you don't use size classes the grey ones are the removed ones. And you need to remove them second time. No matter how stupid it sounds.
If you use size classes, it means that your current size class is different than for greyed constraint. (however the first scenario is also possible...)
In your case it looks like this is the first case. You need to remove greyed constraints once again.

Autolayout contraint error

So this autolayout error. Which I know why it is there but I am not sure how can I fix it. As with error its clear that its not able to resolve the constraints.
In portrait mode all 47 episode are listed and can be scrolled to. But in landscape mode it cant, which is clear as height of table is not changing thats way its is still down there but can't scroll to it.
So this is how I have setup.
Where as view controller has two view which works as placeholder upper is of for iAds and lower is holding table view. Idea was behind this. if ad is not loaded I will set height of View to zero which holds iADBanner. So that Table view takes up all space. (Got this idea from Ray Wenderlich's app level me up.) I was struggling with auto layout initially but I made it right so that view are taking up all width when sim goes to portrait mode. but somehow I am not able to fix the height of view which holds tableView.
Below are the screenshot of constraints.
I tried to be as descriptive as I can. but I am looking for more than just answer. I want to get to know this completely so that I will most likely won't have problem in future. I have finished raywenderlich tutorial already. So Any other pointer would be very much appreciate along with the answer.
2014-07-30 21:40:37.326 Test[85608:60b] 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:0xa5afbc0 UIView:0xa69b7d0.width == 0.682303*UIView:0xa69b830.height>",
"<NSLayoutConstraint:0xa5afc80 V:|-(50)-[UIView:0xa69b830] (Names: '|':UIView:0xa69b7d0 )>",
"<NSLayoutConstraint:0xa5afdc0 V:[UIView:0xa69b830]-(0)-[_UILayoutGuide:0xa5a8410]>",
"<_UILayoutSupportConstraint:0xa5a7d20 V:[_UILayoutGuide:0xa5a8410(0)]>",
"<_UILayoutSupportConstraint:0xa5af490 _UILayoutGuide:0xa5a8410.bottom == UIView:0xa69b7d0.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0xa792d10 h=--& v=--& H:[UIView:0xa69b7d0(480)]>",
"<NSAutoresizingMaskLayoutConstraint:0xa792dd0 h=--& v=--& V:[UIView:0xa69b7d0(271)]>" )
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xa5afdc0 V:[UIView:0xa69b830]-(0)-[_UILayoutGuide:0xa5a8410]>
Break on objc_exception_throw to catch this in the debugger. The
methods in the UIConstraintBasedLayoutDebugging category on UIView
listed in <UIKit/UIView.h> may also be helpful.
The list of constraints in the error log is the key to understanding this type of problem. What you want to do is look at it carefully to correlate which lines refer to which constraints in your code/ui builder, and which hexadecimal address refers to which view.
V:[UIView:0xa69b830]-(0)-[_UILayoutGuide:0xa5a8410] is a constraint setting the bottom of a UIView to a layout guide, so most likely that's the last constraint in your screenshot "Vertical Space - Bottom layout guide - TableVi..." (presumably that's TableViewHolder truncated). That means UIView:0xa69b830 is your TableViewHolder.
V:|-(50)-[UIView:0xa69b830] (Names: '|':UIView:0xa69b7d0 ) is the constraint tying the top of TableViewHolder 50px from its superview, which must be UIView:0xa69b7d0.
UIView:0xa69b7d0.width == 0.682303*UIView:0xa69b830.height looks like an aspect ratio constraint between the superview width and the TableViewHolder height.
h=--& v=--& H:[UIView:0xa69b7d0(480)] and h=--& v=--& V:[UIView:0xa69b7d0(271)] are constraints on the superview that's derived from its autoresizingMask, where the width=480px, height=271px, and their top/left/width/height are fixed (based on h=--& v=--&). This is sort of the standard setup for the root view of a view controller — it's dimensions are managed manually by the view controller to fill the screen.
So once you have all that, you can see what the problem is: the superview has fixed dimensions 480x271. Meanwhile, TableViewHolder's height is being dictated by multiple conflicting constraints:
#1 and #2 are trying to stretch it vertically to fill its superview with a 50px margin at the top, so height = 271-50 = 221px.
#3 is trying to set the height as a ratio of the superview's width: height = 480/0.6823 = 703.5px
221 != 730.5!
Something's gotta give, and the OS just happened to pick #1, so the bottom of TableViewHolder extends past the bottom of the layout guide, making it stick out past the edge of the screen and inaccessible.
Your fix will likely involve getting rid of that aspect ratio constraint, but there might be other issues that appear once you fix that. Good luck!

AutoLayout exceptions iOS6

I am currently using an xib with autolayout to create custom cells in collection view. I have different cell sizes for both orientation. Hence I change the width and height of the cell according to the orientation.
The problem is that when I change the device orientation I get huge number of warnings / exceptions in iOS6.
However when I do the same in iOS7 no warnings are there.
Though it's not affecting my functionality I would like to remove all these warnings.
Here is my XIB file
And following are the errors / warnings /exceptions I am getting.
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:0x164371f0 V:[UILabel:0x16437240(21)]>",
"<NSLayoutConstraint:0x16438050 V:[UILabel:0x16437240]-(1)-[UILabel:0x16437670]>",
"<NSLayoutConstraint:0x16437fd0 V:[UILabel:0x16437670]-(5)-| (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSLayoutConstraint:0x16437f50 V:[UILabel:0x16436e10]-(0)-[UILabel:0x16437240]>",
"<NSLayoutConstraint:0x16437e90 V:|-(7)-[UILabel:0x16436e10] (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16354d00 h=--& v=--& V:[UICollectionView:0xb24f200(264)]>",
"<NSAutoresizingMaskLayoutConstraint:0x163513b0 h=-&- v=-&- LibraryCollectionViewCell:0x16436ae0.height == UICollectionView:0xb24f200.height - 264>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x164371f0 V:[UILabel:0x16437240(21)]>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
I have tried [cell setTranslatesAutoresizingMaskIntoConstraints:NO] but it crashes the app instead.
Can anyone please provide some solution on this?
"<NSLayoutConstraint:0x164371f0 V:[UILabel:0x16437240(21)]>",
"<NSLayoutConstraint:0x16438050 V:[UILabel:0x16437240]-(1)-[UILabel:0x16437670]>",
"<NSLayoutConstraint:0x16437fd0 V:[UILabel:0x16437670]-(5)-| (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSLayoutConstraint:0x16437f50 V:[UILabel:0x16436e10]-(0)-[UILabel:0x16437240]>",
"<NSLayoutConstraint:0x16437e90 V:|-(7)-[UILabel:0x16436e10] (Names: '|':LibraryCollectionViewCell:0x16436ae0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x16354d00 h=--& v=--& V:[UICollectionView:0xb24f200(264)]>",
"<NSAutoresizingMaskLayoutConstraint:0x163513b0 h=-&- v=-&- LibraryCollectionViewCell:0x16436ae0.height == UICollectionView:0xb24f200.height - 264>"
OK, from your constraints you have...
(Numbers in brackets are last 3 digits of reference).
Label (240) with height constraint fixed to 21.
Label (240) with a vertical gap of 1 to label (670).
Label (670) with a vertical gap of 5 to the superview (ae0) bottom.
Label (e10) with a vertical gap of 7 to the superview (ae0) top.
UICollectionView (200) with fixed height of 264.
LibraryCollectionViewCell (ae0) with an equal height to the collection view - 264 (200).
< Adam Savage >OK! There's your problem!< / Adam Savage >
The last constraint in the list will give the cell a height of 0. (264 - 264 = 0)
However, in the cell you have a label with fixed height 21 and fixed vertical gaps of 7, 5 and 1.
264 - 264 = 0
21 + 7 + 5 + 1 = 36
36 != 0
You can fix this a number of ways. Remove the constraint between the cell and the collection view. Remove the fixed height constraint on the label. etc...
I'd recommend drawing out how you want it to work (on paper) and scribbling in the constraints that you need. Or put a picture of what you are trying to do into your question so we can try to help.
A lot has changed in the way Auto layout takes place from iOS 6.0 to iOS 7.0.
This is quite evident from auto layout implementation via X-Code 4.x & X-Code 5.x.
Since Apple is now expecting apps to be built in X-Code 5.x, I would suggest to go with the same.
Check for the base SDK as well.

Correctly define constraint for UITableViewCell resizing

i'm currently facing this problem, i need to define a dynamic height UITableViewCell.
The red lines correspond to resizable views (will grow vertically), the blue label have 3 constraints: two for each immediate red views above (>= 8), one more with 170 and lower priority to superview.
The green one has a bottom constraint to the superview (5)
When i try to run my code, it gives me 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x19d65bb0 V:[UIView:0x19d9a9a0(5)]>",
"<NSLayoutConstraint:0x19d667d0 V:[UILabel:0x19de4180(19)]>",
"<NSLayoutConstraint:0x19d5ba70 V:[UILabel:0x19d61870(19)]>",
"<NSLayoutConstraint:0x19d36df0 V:[UILabel:0x19d36e80(19)]>",
"<NSLayoutConstraint:0x19d35d30 V:[UIView:0x19d36130(0)]>",
"<NSLayoutConstraint:0x19e7b310 V:|-(5)-[UILabel:0x19d6b520] (Names: '|':UITableViewCellContentView:0x19d97580 )>",
"<NSLayoutConstraint:0x17d0b060 V:[UILabel:0x19d6b520]-(5)-[UIView:0x19d9a9a0]>",
"<NSLayoutConstraint:0x17d0a480 V:[UIView:0x19d9a9a0]-(8)-[UILabel:0x19de4180]>",
"<NSLayoutConstraint:0x17d0c920 V:[UILabel:0x19de4180]-(8)-[UILabel:0x19d61870]>",
"<NSLayoutConstraint:0x17d0a100 V:[UILabel:0x19d61870]-(8)-[UIView:0x19d839e0]>",
"<NSLayoutConstraint:0x17d11ad0 V:[UIView:0x19d839e0]-(8)-[UILabel:0x19d36e80]>",
"<NSLayoutConstraint:0x19e742a0 V:[UILabel:0x19d36e80]-(8)-[UILabel:0x19d36770]>",
"<NSLayoutConstraint:0x19ed84a0 V:[UILabel:0x19d36770]-(8)-[UIView:0x19d36130]>",
"<NSLayoutConstraint:0x19e76410 V:[UIView:0x19d36130]-(>=8)-[UILabel:0x19d35560]>",
"<NSLayoutConstraint:0x19ed8af0 V:[UILabel:0x19d35560]-(8)-[UITextView:0x1832ba00]>",
"<NSLayoutConstraint:0x19e7abf0 V:[UITextView:0x1832ba00]-(8)-[UILabel:0x19e7d890]>",
"<NSLayoutConstraint:0x19e83d90 V:[UILabel:0x19e7d890]-(8)-[UITextView:0x18a95e00]>",
"<NSLayoutConstraint:0x19e7b520 V:[UITextView:0x18a95e00]-(5)-| (Names: '|':UITableViewCellContentView:0x19d97580 )>",
"<NSAutoresizingMaskLayoutConstraint:0x19d46fc0 h=--& v=--& V:[UITableViewCellContentView:0x19d97580(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x19d36df0 V:[UILabel:0x19d36e80(19)]>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
So, the question is:
What is the correct way of achieving this???
Are u using storyboard, if yes, simple go to Editor->Resolve Auto Layout Issues->Clear All Constraints In ........ Controller, and then Add Missing Constraints In ...... Controller
It will help you fix the problem of breaking contrainsts between view

Resources