Auto Layout of view hides subviews - ios

I have a UIViewController embedded in a Navigation Controller. I am using autolayout. In interface builder this is how the VC looks.
But when run in the simulator I get this:
With this error.
2016-08-10 16:51:17.077 My App[11797:7752671] 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) (
"<_UILayoutSupportConstraint:0x7fad72f7d6b0 V:[_UILayoutGuide:0x7fad72e5d1d0(64)]>",
"<_UILayoutSupportConstraint:0x7fad72ffa6a0 V:|-(0)-[_UILayoutGuide:0x7fad72e5d1d0] (Names:
'|':UIView:0x7fad72fe1b40 )>",
"<_UILayoutSupportConstraint:0x7fad72fe2460 V:[_UILayoutGuide:0x7fad72e64760(0)]>",
"<_UILayoutSupportConstraint:0x7fad72fe1470 _UILayoutGuide:0x7fad72e64760.bottom == UIView:0x7fad72fe1b40.bottom>",
"",
"",
"",
"")
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.
I have rebuilt this view several times. I go up to Editor > Resolve Auto Layout Issues > "All Views" Reset to Suggested Constrains.
Which generates this:
I have even manually deleted all constrains and started over.
But nothing works.
I have nothing in the .m file that lays anything out or adjust sizes or locations. Nothing fancy is happening with this view. What am I doing wrong?

Editor > Resolve Auto Layout Issues > "All Views" Reset to Suggested Constrains almost never works correctly. So just avoid it.
When adding constraints, you need to specify four things for every item:
X location
Y location
Width
Height
There are creative ways to do this, so for you I would suggest you add these constraints to the Lookup Button:
Horizontally in center
Leading Space To Superview or Trailing Space to Superview (you do not need both)
Height
Top Space to Call Number
Your x location and width are satisfied by Horizontally in Center and Leading or Trailing Space. These two constraints center the button and give you equal space on either side of the button. The button will be as wide as the view minus the space on both sides. Setting the height obviously satisfies the height of the button and Top Space satisfies the y position of your button. Therefore you have easily satisfied x location, y location, width and height of the button, and you are done.

plz set your constraints as following image

You need to start over and think about constraints you are making. Think about what information is known, what sizes should not change. Tell this information to Auto Layout.
Your button is not appearing because Auto Layout does not known how to position it, it missing following numbers. Delete all constraints and start over with the following. Not you can change this numbers.
Txt Search.top = Top Layout Guide.bottom. This is yours, it is correct.
Txt Search.height = 20.0
Txt Search.leading = 0 with margin
Txt Search.trailing = 0 with margin
Lookup.top = Txt.Search.bottom + standard margin
Lookup.leading = 0 with margin
Lookup.trailing = 0 with margin
Lookup.height = 20.0

Reset to suggested constraints never solves your problem, it only hides the warning. You have to set the constraints that suits for your requirement. From your question, when you choose the option, Reset to suggested constraints, the autolayout will give a bottom layout constraint for the LOOK UP button,and it had too much of bottom constraint(which happens when you rest to suggested constraint), hence it is pushed up in the runtime.
You just need top and leading/ top and center horizontal constraint for your button corresponding to its above textfield along with height and width constraint. Remove the bototm constraint for your LOOK up button.

Related

NSLayoutConstrains error occurs when reorder the UITableViewCell

I set a simple cell in storyboard,
Thers's a imageview and a label, I set some constrains, it's imageView's leading ,top and bottom space to contentview, and the imageView's width and height, and the label's horizontal spacing to imageview and vertically center to imageview.
When I run the app, it shows correct, no error occurs.
The rightBarButtonItem is the system edit button. When I reorder the cell, constrain error occurs, I do not know how to fix it.
2015-11-11 23:08:05.681 HiPDA[10108:2305919] 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:0x7fce60ce6cf0 V:[UIImageView:0x7fce60cac370(40)]>",
"<NSLayoutConstraint:0x7fce60ce6f10 UITableViewCellContentView:0x7fce60ca0610.topMargin == UIImageView:0x7fce60cac370.top>",
"<NSLayoutConstraint:0x7fce60ce7020 UITableViewCellContentView:0x7fce60ca0610.bottomMargin == UIImageView:0x7fce60cac370.bottom - 0.5>",
"<NSLayoutConstraint:0x7fce60c03a70 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fce60ca0610(56)]>"
)
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 <UIKit/UIView.h> may also be helpful.
I was confused, when it showed, scrlled and removed, no errors occur, but when I reordered the cell, constrain error occurs, I do not know how to fix it. Thank you.
I upload the project, you can download here download link
There's are two navigation controllers, change the init controller between them, when the tableviewcell contains only label no error occurs, but when the tableviewcell contains imageview error occurs when reorder cell.
You gave imageView two Sets of constraints to determine it's height ,one is top&& bottom space, another is height.
When they conflicts with each other, you will get this alert.Change one of them to optional will fix the problem.
In you case ,You can change your imageView's height constraint from "=" to ">=" or lower height constraint's priority to 999.
Looking at your constraints the conflict is that
The imageview height is set to 40, it is pinned to the top and bottom of the cell, while the cell height is 56. how big do you want the imageview 40 or 56?
You can solve this by removing the top and bottom constraints and just do a center vertical to the cell constraint. i.e. ImageView.centerY = centerY
Here's modified constraints for your project on the image view

Strange autolayout behaviour on collection views

I have an issue with autolayout, the console is reporting problems with an image view I have in a cell:
RefreshCatalogue[31754:16177989] 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:0x7fa98103b740 V:[UIImageView:0x7fa9810375d0]-(0)-| (Names: '|':UIView:0x7fa9810371d0 )>",
"<NSLayoutConstraint:0x7fa98103b7e0 V:|-(0)-[UIImageView:0x7fa9810375d0] (Names: '|':UIView:0x7fa9810371d0 )>",
"<NSLayoutConstraint:0x7fa98103b8d0 UIImageView:0x7fa9810375d0.centerY == UIImageView:0x7fa981037490.centerY>",
"<NSLayoutConstraint:0x7fa98103ba60 UIImageView:0x7fa981037490.top == UIView:0x7fa9810371d0.topMargin + 71>",
"<NSAutoresizingMaskLayoutConstraint:0x7fa980d44a10 h=--& v=--& V:[UIView:0x7fa9810371d0(50)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa98103b8d0 UIImageView:0x7fa9810375d0.centerY == UIImageView:0x7fa981037490.centerY>
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 problem is that if I do:
[self.collectionView reloadItemsAtIndexPaths:#[indexPath]];
Nothing shows even tho the image view seems to have correct frame, the image property is set.
If I reload the entire collection view with:
[self.collectionView reloadData];
The error is still there but the image shows. The code is open here shall anyone be interested in taking a look:
https://github.com/Ridiculous-Innovations/RefreshCatalogue
Also, all the constraints in the story board seem to be in blue. Any idea what might be causing the issue?
Edit: Needless to say that all the elements, including the image view are on the right place (I did debug frames and set random colours) but the image didn't display till refresh ... sometimes the cells don't display at all
You have issue with UIImageView:0x7fa981037490.top == UIView:0x7fa9810371d0.topMargin + 71 and parent frame with 0 height (so imageview height must be negative, but this is not allowed), like right after calling dequeueReusableCell method.
Possible workaround for this case is change priority from 1000 to 999 on the most bottom constraint in nested view (Vertical Space - catalogueHeaderCell - Image View and Vertical Space - catalogueCell - Info View).
In second cell some constraints are disabled. that means that constraints are removed for Any Height Any Width. so when you run app in iphone than that constraints conflict with your current constraints.
so remove that highlighted disable constraints.
Try this it helps me in removing those constraint break in console.
yourCustomCell.contentView.frame = yourCustomCell.bounds;
Views:
UIView:0x7fa9810371d0, let's name it container
UIImageView:0x7fa9810375d0, let's name it image1
UIImageView:0x7fa981037490, let's name it image2.
Not let's inspect the constraints
Container height is 50, specified by autoresizing. The height is probably not specified by your collection view. Most likely this is the size of the cell when it gets created before it gets resized to the size specified by your collection view layout.
image2 is 71 points under the topMargin of container, so it is at least 21 points under bottom edge of the container.
image1 bottom is aligned with the bottom of the container, so it is above the bottom edge of the container.
Now your last constraint centers image1 and image2 vertically so there is obviously a collision.
It's a bit hard to know what you are trying to do but I suppose you should position only image1 relative to the container and the only vertical constraint for image2 should be the ones that centers them vertically.
Too see the errors in Interface Builder, change your cell height to 50, you will see everything broken up. There are just some unnecessary constraints which cause conflict on resizing.
There are also some constraints that are not installed for the current size class. It's possible that you are running the app on a simulator that is of a different size class and has them installed. That means there are even some additional collisions.
check your imageView centerY constrain
delete this constrain...
this problem is occur because of constrain ambiguity. while adding auto layout you have to apply proper auto layout constrain to imageView.
After deleting centreY constrain if there is ambiguity then xcode will again show you same error, again you have to delete ambiguous constrain.
Well the warning message gives a clue to what is wrong.
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa98103b8d0 UIImageView:0x7fa9810375d0.centerY == UIImageView:0x7fa981037490.centerY>
So to analyse you have two view based objects. One is set to centre its self from top to bottom to the other view (0x7fa9810375d0 to 0x7fa981037490 - these are the views id's)
But also view id 0x7fa981037490 has another constraint,
<NSLayoutConstraint:0x7fa98103ba60 UIImageView:0x7fa981037490.top == UIView:0x7fa9810371d0.topMargin + 71>
Which looks like the top of 0x7fa981037490 is being aligned to the top of view 0x7fa9810371d0 plus 71 points.
I haven't downloaded your project but I'd bet that's where the problem lies. Best guess is the centre.y constraints cannot be satisfied because the align top moves the ImageView into a place that breaks it. Try to look at what you have set in constraints visually in your head. You have aligned to ImageViews and aligned one of those to the edge of another view. How will that break? (is what you should be asking yourself)
Try removing a constraint one at a time and build and run. If the warning goes, thats the one that needs fixing. Yes the view might look weird and you can use Command + Z to undo the deletion of the constraints one by one.
Give it a go. Let me know

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.

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.

Resources