Auto layout constraints in code conflicting with sizes in XIB - ios

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.

Related

Disabling Auto Layout For A Single UIImageView

I have a UIImageView in my photo app that changes size based upon the imported image. The size is created dynamically and then positioned programmatically in the center of the screen without using auto layout.
Now to do this I have used
self.imageViewCanvas.translatesAutoresizingMaskIntoConstraints = YES;
[self.imageViewCanvas setFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
to disable the constraints. The problem is that whenever I load the app I get a long warning message that contains the following:
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)
(
"",
""
)
Will attempt to recover by breaking constraint
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
Is there a way to disable this particular warning or can I adjust the code in some way to avoid it?
Thanks
For a storyboard or a NIB with auto layout enabled, Xcode will provide constraints that are missing. This happens at build time. If you look at the Size inspector for a view to which you haven't added any constraints, you'll see a message about Xcode adding constraints for you. So, basically, no matter what you do, the image view will be constrained.
If you then set translatesAutoresizingMaskIntoConstraints to true, that will conflict with the automatically-supplied constraints. You could remove/deactivate those constraints, but it's hard to obtain references to them.
Your options:
Use auto layout to position the image view. It's easy to accomplish what you want.
Turn off auto layout for the whole NIB, so there will be no constraints and translatesAutoresizingMaskIntoConstraints will be true by default.
Add sufficient constraints of your own in the NIB but mark them to be removed at build time on the Attributes or Size inspector. This signals to Xcode that you want to take over and prevents it from supplying its own constraints. (This is normally done when you will be supplying constraints programmatically, but it can also work if you're just going to turn on translatesAutoresizingMaskIntoConstraints.)
Add constraints of your own and set up outlets to them. Deactivate them programmatically before turning on translatesAutoresizingMaskIntoConstraints.

Adding a button in dynamic TableViewCell

I have a label of dynamic size in a TableViewCell. I have pinned it to up,down,left and right and set Lines to 0 and it is working fine. Next, I add a button below the label and and pin it to top and left. and fix the width and height. But I am getting an error at runtime
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:0x7fcbc302f600 UILabel:0x7fcbc3034010'Digestion of food in the...'.top == UITableViewCellContentView:0x7fcbc30334a0.topMargin>",
"<NSLayoutConstraint:0x7fcbc3037e30 UITableViewCellContentView:0x7fcbc30334a0.bottomMargin == UILabel:0x7fcbc3034010'Digestion of food in the...'.bottom + 38>",
"<NSLayoutConstraint:0x7fcbc303a920 'UIView-Encapsulated-Layout-Height' V: [UITableViewCellContentView:0x7fcbc30334a0(43.5)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fcbc3037e30
UITableViewCellContentView:0x7fcbc30334a0.bottomMargin ==
UILabel:0x7fcbc3034010'Digestion of food in the...'.bottom + 38>
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. Here is the screenshot of my layout
Which constraint should I remove? I don't think I have added extra constraints
In interface builder check that there are no warnings (the yellow arrow on the side of the vc inspector) and check that the height of the label +the button and the margins don't exceed the cell height.
Also you said you pinned to the top, of what? The container or the label?
Use these constraint for as per your requirements:
OR
Follow these links Autolayout or Autolayout programatically
(OR)
For some reason, Xcode generates own default set of auto layout constraints on a NIB at build time. This is why I couldn't add any more manual constraints, because they were conflicting with the automatically added ones.
I resolved this the following way:
Open up the Storyboard view controller you're handling.
Select the view controller and select Editor > Resolve Auto Layout Issues > All Views in [ ] View Controller > Add Missing Constraints from the menu:
This will ensure that no additional build time constraints are created and all the constraints are now visible.-->Select all the constraints from your view controller:
This will ensure that no additional build time constraints are created and all the constraints are now visible.-->Check from the right pane the following check box: Placeholder - Remove at build time:
(This will ensure that no additional build time constraints are created and all the constraints are now visible.)
Now you can add all the auto layout constraints manually in the code.
you will have to provide some more info than that regarding the values you set in auto layout like the distance of the UILabel from top margin,down,left etc.
The problem might be that you specified the UIButton to be from top margin , it should be with respect to your UILabel as its below it so set the UIButton's vertical spacing property to the UILabel this makes sure the UIButton is placed below it.

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.

ios 6 auto layout constraints error

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.

Resources