Constraints error: Need Constraints for Y Position or height

I am learning to use the constraints system in Xcode, and it is pretty cool. But I am getting some weird errors I don't understand. For example this "View X Needs Constraints for Y Position or Height".


This view is within a StackView, which I thought sort of automatically determined its subviews widths and heights. But in order to try to eliminate this error, I added a constraint setting the top to equal the top of the stackview -- that's the same as a Y position, right? And I don't want a static height so I added a Height >= 40 and Height is <= 56 constraint. Shouldn't those all be enough to get rid of the warning? It's still there.

Replies

AutoLayout is in fact solving linear equations.

So, AFAIK:

With Y and Height >= 40 and Height is <= 56, it is not able to decide, so it will use the size given in IB, but let you know.


What should be the value of height (I mean the formula : total stack height / number of items )?

But layout does not accept formulas, so

- you could define a defaut constraint (eg 50)

- create an IBOutlet for the constraint

- set its value (constant) by applying the result of the formula


There is probably a simpler way to silence the warning, but tonight cannot think of it.

PS: it's just a warning, you could also ignore if that works OK. But I agree, it is distracting.

I guess I need to study up more on how constraints work. I mean, it doesn't complain if you just drop three or four blank views into a stack view with no constraints at all, does it? It just divides them equally or let's the last one soak up the extra space. I figured with the extra constraints I gave it, it would have an easier time, not a harder one. or maybe it does complain in the former case and I just never noticed.

You're right.


If you do not create constraint, it manages itself.

But when you create a first one (like x position), autoLayout strats to resolve and ask for missing data.

So, I personally avoid creating constraints if not needed (and inside a stack view, that's probably not needed) ; just set the constraints to position nthe stackview itself.

Man these constraints have me tearing my hair out. They just don't make sense much of the time. I think it might be buggy actually. For example why would a stack view ever lay out its subviews such that they run outside of the stack view? It's also confusing because the layout that XCode displays on the storyboard is often incorrect, which is misleading. You have to run it on the simulator or a device to see what it really thinks.

I understand. You're not the only one who got headache with autolayout. It's not (much buggy) but it is definetely complex.


After time, I find it very powerfull, but I apply now a few "rules" ito ease the use:

- declare constraints when they are really necessary (often the case in IOS because of different sizes)

- do not try to create all constraints in IB. If they become too complex, build some programmatically (I had to do it for a window that could be presented landscape or portrait, with many objects ; it was nighmare in IB and I could not visualize the result anyway ; but I needed constraints because window could change size (OSX)

- give a label to constraints of a given object (like #01, #02, …) : that helps a lot debugging

- when warnings are impossible to silence, think of changing priority.

It would be great if it just worked 100%. Like right now, I have no warnings and no errors -- but my stackview is laying out it subviews so that they extend beyond the end of the stackview. It shouldn't do that.

I had the same problem. In fact IB (xcode) shows error but you'll get no error when running. This is because IB can't infer our content size since it's sets when the running.

To silence this error, you could place a place holder size in IB for your stackview items. this is juste for IB purpose, will not conflict with your final layout.