ios 6 auto layout constraints error - ios

I am having an error in my iPad project with auto layout constraints. It's happening with a custom UICollectionViewCell I've created in IB, when I rotate the device to landscape. The thing is that none of the constraints are deletable. I'm still learning auto layout, so I'm sure it could just be a matter of adding some restraints and deleting some of the default ones, but I'm stuck. It's definitely happening with my custom cell - when I remove it and use a plain old UICollectionViewCell I get no error.
I searched around and I tried setting translatesAutoresizingMaskIntoConstraints on my custom cell to NO but then I get the following error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UICollectionView's implementation of -layoutSubviews needs to call super.'
Works fine to set it to NO on the subviews of my custom cell, but when I set it on the cell itself it barfs.
Here is the auto layout constraint error I'm getting - it's the usual one:
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)
(
"NSAutoresizingMaskLayoutConstraint:0x76bf950 h=-&- v=-&- CMAGalleryCollectionViewCell:0x7687690.height == UICollectionView:0x9161e00.height - 875",
"NSAutoresizingMaskLayoutConstraint:0x76693c0 h=--& v=--& V:[UIView:0x7665470(704)]",
"NSLayoutConstraint:0x7665850 UICollectionView:0x9161e00.bottom == UIView:0x7665470.bottom",
"NSLayoutConstraint:0x7665740 V:|-(0)-[UICollectionView:0x9161e00] (Names: '|':UIView:0x7665470 )"
)
Will attempt to recover by breaking constraint
NSLayoutConstraint:0x7665850 UICollectionView:0x9161e00.bottom == UIView:0x7665470.bottom
Any help or suggestions greatly appreciated.
Thanks!

I've hit this a lot. When you rotate the device/application the width/height of the view change to fit the new orientation screen, and that often breaks a "width = 320" type constraint.
I found the exception above to be unreadable until I'd read this:
http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/formatLanguage.html
... which is similar to the format shown, and then it became quite easy to see what was a problem.
The non-deletable constraints you are seeing can be altered. You can lower the priority from 1000 to something that can be broken or you can replace them with something that works. One (imperfect) approach might be to make a rule >= 320 so things could stretch for landscape, or you could add your own constraints to give (perhaps tying the view to it's superview, and then the width constraint should be deletable.) That said, that is not a good solution.
I'd recommend you typically attempt to remove width and height constraints where at all possible on something (like a large collection view) that should react to the shape/size of the main view 'cos those widths/heights will typically be wrong 50% of the orientations.
That all said, is this saying the cell height is collection height minus 875?
CMAGalleryCollectionViewCell:0x7687690.height == UICollectionView:0x9161e00.height - 875",
Is this cell a sub view of the collection view and not a template for cells? I've had that problem with interface builder and collections also. I had to rebuild the collection view from scratch in IB to not add a bogus child cell.

Related

NSLayoutConstraint errors only when switching view controllers

I'm getting some very weird behaviour from one of my view controllers.
I'm navigating to the same controller (with different instances of it) from two different UITableViews in different tabs.
When it gets displayed from a tap on a UITableViewCell, everything is fine and no errors display. If I have the controller open and I navigate away from it by selecting another tab, then navigating back to it through pressing the original tab, I get a large amount of NSLayoutConstraint errors.
I don't actually have any constraints in my cell and I don't have any UI elements on the cell's content view in the Storyboard, as I'm adding all of them programmatically. I'm not defining any constraints in the code either.
The full error is here, as it's too large to reasonably paste into SO.
An excerpt is as follows:
2015-04-11 16:49:23.889 TradingPost[6982: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:0x155e5250 H:[UILabel:0x155d8150(158)]>",
"<NSLayoutConstraint:0x155ed9e0 H:|-(8)-[UILabel:0x155d8150] (Names: '|':UITableViewCellContentView:0x155d4a80 )>",
"<NSLayoutConstraint:0x155eda30 H:[UILabel:0x155d8150]-(47)-[UIView:0x155e01a0]>",
"<NSLayoutConstraint:0x155f1bc0 UIView:0x155e01a0.trailing == UITableViewCellContentView:0x155d4a80.trailing - 8>",
"<NSAutoresizingMaskLayoutConstraint:0x156b7640 h=--& v=--& H:[UITableViewCellContentView:0x155d4a80(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x155e5250 H:[UILabel:0x155d8150(158)]>
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.
Addition: Upon further investigation, I find I'm still getting errors, even when I remove all UI generating code from the controller and all constraints from the Storyboard. At this point I'm suspecting witchcraft.
Even if you don't create any constraints explicitly, the auto layout system can create them from the autoresizingMask mask associated with each view. You can try setting translatesAutoresizingMaskIntoConstraints to NO. However, if you're using auto layout and size classes to create an interface that adapts to various screen sizes, you'll probably need to embrace constraints. In that case, you might be better off setting the relevant constraints explicitly in your code.
I found that another view, attached to the one that I thought was causing the trouble, was the actual cause of the problem.
All it needed was a reset of the constraints to fix it, I just completely forgot about it when I encountered this problem.

Using auto layout and systemLayoutSizeFittingSize

I have a really simple view that I cannot for the life of me figure out the problem. I have successfully used auto layout and systemLayoutSizeFittingSize for fairly complex custom UITableViewCells. This is my first time using it for a UIView (to be used in a UITableView.tableHeaderView) and I cannot get it to work.
I have a simple label in the view that I want to cover the entire view. I have added the Trailing, Leading, Top, and Bottom constraints to the label against the parent's corresponding edges. When I do this, systemLayoutSizeFittingSize computes the correct height, but I get the following error:
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:0x7fbb71d9a310 V:|-(5)-[UILabel:0x7fbb71df5120'Test Label'] (Names: '|':mailapp.MailTableHeaderView:0x7fbb71d2c490 )>",
"<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>",
"<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fbb71d2ebf0 UILabel:0x7fbb71df5120'Test Label'.bottom == app.HeaderView:0x7fbb71d2c490.bottom - 5>
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.
Now, if I remove the "bottom" constraint on the label, auto layout works correctly but now systemLayoutSizeFittingSize returns 0.
What is the right way to make the constraints satisfy the auto layout engine but also to have systemLayoutSizeFittingSize compute the correct size?
As I mentioned, the most baffling thing is that I have used this successfully for complex auto layouts, but I noticed all those were UITableViewCells. I'm not sure if there is a difference.
I figured out the problem, but I don't understand why. For the UITableViewHeader, I cannot do the typical auto layout thing for the view. That is, I have to set translatesAutoresizingMaskIntoConstraints to be true. Effectively, I cannot use auto layout for the header view, and I have to set the frame manually.
Within the header view, I can use auto layout for the sub views successfully.
So my algorithm goes like this for constructing the tableHeaderView:
Create the header view with any arbitrary frame
Compute the constraints (of the header view's subviews)
Set the frame.width of the header view to match the parent tableView.frame.width
Layout the header view
Use systemLayoutSizeFittingSize to compute the height
Update the header view's frame.height
This works for me. I was not able to get it to work if I created the header view with translatesAutoresizingMaskIntoConstraints set to false.
As per the Apple Visual Format Language the following appears to be setting a '0' height constraint on your HeaderView.
E.g.
"<NSLayoutConstraint:0x7fbb71de31d0 V:[app.HeaderView:0x7fbb71d2c490(0)]>"
I would start by investigating this, as having a height of 0 will likely mess up your other label constraints for top and bottom spacing.

How to tell UITableView that a self-sizing cell's height is about to change?

I've got a UITableView whose contents are somewhat dynamic. It seems like every time I resize them, I hit this error:
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:0x15dbabf0 WKWebView:0x15da1650.top == UITableViewCellContentView:0x15eaedb0.topMargin>",
"<NSLayoutConstraint:0x15dbac20 UITableViewCellContentView:0x15eaedb0.bottomMargin == WKWebView:0x15da1650.bottom>",
"<NSLayoutConstraint:0x15db1360 V:[WKWebView:0x15da1650(158)]>",
"<NSLayoutConstraint:0x15ed0c00 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x15eaedb0(43.5)]>"
)
It's that last one - the one labeled UIView-Encapsulated-Layout-Height - that is breaking things. I want to get rid of it, or set it to a breaking priority, or something - but I can't seem to be able to tell UITableView to break it. I've tried every possible combination of setNeedsUpdateConstraints and setNeedsLayout on various views. I've tried removing all the constraints from the cell's contentView on updateConstraints. I've tried just calling beginUpdates and endUpdates - in that case, updateConstraints is never called & the constraints are not re-evaluated.
I'm at a loss, here. Without self-sizing cells, just calling begin/endUpdates used to trigger a re-calculation of the cell's dimensions. How do I do it now?
The WKWebView which you are using inside the content view is the most possible cause of breaking constraints. Also the height of any webView can be calculated only when the WebView has finished loading. For e.g in UIWebView the height of the UIWebView can be fetched appropriately only in the delgate method "didFinishLoadingWebView" method.
Also, the 'UIView-Encapsulated-Layout-Height' constraints supposedly does not get satisfied as the cell's intrinsic height calculated probably has a different value. Hence one can lower the priority of the relevant cell constraint to allow UIView-Encapsulated-Layout-Height to have precedence.

Auto layout constraints in code conflicting with sizes in XIB

I've set up a number of UILabels and UIButtons within 3 subviews in a storyboard. In code, I've defined auto layout constraints for all of these however for some reason when I run the app, the sizes that I have defined in the storyboard are conflicting with the constraints in code.
For example, one subview is positioned at 0,0 in the XIB with height 200 and width 320, simply for me to layout the elements before writing the code. There are no constraints in the storyboard.
This subview has a number of UILabels and UIButtons within it and their cumulative height is supposed to define the height of the subview. This should end up at 205pts height, however the log shows a conflict:
2014-06-02 16:45:38.506 discounter[11691: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)
(
"<NSIBPrototypingLayoutConstraint:0x109390160 'IB auto generated at build time for view with fixed frame' V:[UIView:0x109389010(200)]>",
"<NSLayoutConstraint:0x109249510 V:[UIView:0x109389010(205)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x109249510 V:[UIView:0x109389010(205)]>
I have set all my views to have translatesAutoresizingMaskIntoConstraints = NO so I'm at a loss as to why this is happening. It appears to be happening to a number of other elements too, but I have a feeling its probably the same reason why.
Can anyone help?
Thanks!
Add those constraints that you will replace in your code in your storyboard, and check their "remove at build time" properties. Like this:
Background:
This is a way for you to promise Xcode that you will add the constraint in code, and thus will prevent Xcode from auto generating the necessary constraints. The auto generation is necessary, as otherwise the runtime wouldn't be able to determine how to present the view in question. Generally, you should strive to define all your constraints in the storyboard. You could also IBOutlet a constraint to your code, and then edit its constant value when the app is ran, that way avoiding the tedious adding of constraints in your code.

AutoLayout constraint issue with unexpected NSAutoresizingMaskLayoutConstraint

I'm using auto layout constraints programmatically and I am constantly seeing the same kind of error across my application usually related to a constraint that looks like this:
"<NSAutoresizingMaskLayoutConstraint:0x82da910 h=--& v=--& V:[UITableViewCellContentView:0x82d9fb0(99)]>"
I've put some sample code to reproduce at https://github.com/nicolasccit/AutoLayoutCellWarning
In this example, I am creating a very simple view with 2 UI elements: an image view called imageThumbnail and a label called labelName with some constraints:
"H:|-padding-[_imageThumbnail(==imageWidth)]-[_labelName]";
"V:|-padding-[_imageThumbnail(==imageHeight)]-padding-|";
"V:|-padding-[_labelName]";
On both elements I set the AutoresizingMaskIntoConstraints to NO.
And I am getting the following exception:
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:0xa6e4f90 V:[UIImageView:0xa6e4340]-(10)-| (Names: '|':UITableViewCellContentView:0xa6e4150 )>",
"<NSLayoutConstraint:0xa6e4f10 V:[UIImageView:0xa6e4340(80)]>",
"<NSLayoutConstraint:0xa6e4ed0 V:|-(10)-[UIImageView:0xa6e4340] (Names: '|':UITableViewCellContentView:0xa6e4150 )>",
"<NSAutoresizingMaskLayoutConstraint:0xa6e4ac0 h=--& v=--& V:[UITableViewCellContentView:0xa6e4150(99)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xa6e4f90 V:[UIImageView:0xa6e4340]-(10)-| (Names: '|':UITableViewCellContentView:0xa6e4150 )>
I know the last constraint is related to the content view but I am unclear to properly remove it (Setting
AutoresizingMaskIntoConstraints to NO on the contentView raises an error and in the SO link below, it messes up the entire layout):
<NSAutoresizingMaskLayoutConstraint:0xa6e4ac0 h=--& v=--& V:[UITableViewCellContentView:0xa6e4150(99)]>
I've seen the answers at: Auto layout constraints issue on iOS7 in UITableViewCell but none of them seem to be working for me here.
I believe that the constraints I define are valid and pretty straightforward but can't seem to figure out what's going on. And I'm seeing the exception being raised both in iOS 6.1 and iOS 7.
Any idea what I am doing wrong here?
Thanks,
Nicolas
You should read the exception description more thoroughly:
Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints
In short, this constraint you are seeing is due to some UIView having it's translatesAutoresizingMaskIntoConstraints set to YES. In this case I would suspect this is the content view of the cell, as hinted to by UITableViewCellContentView.
You can disable it by just setting the property to NO.
cell.contentView.translatesAutoresizingMaskIntoConstraints = NO
EDIT:
Now, keep in mind that this is a temporary fix, most likely you have some other logic error with your constraints, for example constraining something in the contentView of the cell to the cell itself. Or by seemingly forcing the contentView to be larger than the cell is (and therefore larger than its' automatic sizing is).
For example, is your cell tall enough? i.e is it tall enough so that the contentView is 100pt tall? Note that the contentView has to be that tall, which might not necessarily match the height of the cell.
I've put a corrected version of your code at https://github.com/mattneub/AutoLayoutCellWarning. Works perfectly. This line was the main cause of your trouble:
NSString *const kImageVertical = #"V:|-padding-[_imageThumbnail(==imageHeight)]-padding-|";
Change that to
NSString *const kImageVertical = #"V:|-padding-[_imageThumbnail]-padding-|";
And all will be well.
The main reason you were having trouble is that by assigning an absolute height to the image view, you were making it impossible to also assign a height to the cell. Floating point is not exact, so you need to allow some room for the cell to grow / shrink. If we take away the absolute height, the image view gets its height from its intrinsic content size, at a lower priority, so there is no conflict.
I have some other critiques of your code. In trying to do dynamic setting of the cell's height while using auto layout, you were giving layout and constraint update commands you should never be giving, and you are giving them at wrong times. It is possible to do dynamic row heights based on constraints, but the way you're doing it is not the way. All you have to do is call systemLayoutSizeFittingSize to find out the correct cell height. Also, there is absolutely no need to put your "hidden" cell into the interface. Don't do that; it just confuses things. One of the things you'll notice when you look at my version of the code is that it is much simpler than yours, because of those differences.
For a working method, see my example at https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch08p424variableHeights/ch21p722variableHeights/RootViewController.m
And see the discussion of this issue in my book.
EDIT (May 2014): Unfortunately my answer above fails to point out one of the key causes of this problem, namely, that the cell separator has height (if it hasn't been set to None). Therefore if you assign the cell a height that doesn't take the separator height into account, the auto layout constraints, if absolute, cannot be resolved into that height. (See this answer, which really made the lightbulb come on inside my head.)

Resources