15

screenshot

I want to have the bottom view be at least 20 away from both the image and the label above it. The label is multiline, so it can be taller or shorter than the image view, depending on how much text there is. When I add two "distance to nearest neighbour >= 20" constraints like shown in the screenshot, Xcode tells me constraints are ambiguous.

How do I fix it?

Alex B
  • 82,554
  • 44
  • 203
  • 280
  • How do you want the label to line up with the image view? If it's a single line, should it line up with the top? The bottom? The middle? As it gets taller, do you want the tops to line up until it's too tall to stay 20 away from the bottom view, and then move up? There are several scenarios you could envision, and they all require different solutions. You should be more explicit about what you want. – rdelmar Nov 20 '13 at 05:13
  • @rdelmar they are not aligned relatively to each other vertically (there is another label above this label, and both image and top label are aligned against superview top border). Effectively their tops line up and bottoms vary. – Alex B Nov 20 '13 at 05:15
  • Then, if the label gets too long, do you want the bottom view to get shorter, or move down? Also, does the image view have a fixed height? – rdelmar Nov 20 '13 at 05:19
  • @rdelmar bottom view is pushed down, image is fixed height. – Alex B Nov 20 '13 at 05:22

1 Answers1

27

I think you can do it like this, if I understand your requirements:

enter image description here

The image view has a fixed width and height, and constraints to the left side and top, as well as a constraint to the bottom view of =20 with a priority of 700. That's crucial -- that will set the y position of that bottom view (which has fixed height and constraints to the two sides), but will allow it to move lower if another constraint with higher priority makes it. That constraint with higher priority is the constraint to the label -- it's >=20 with priority of 1000 (the label also has constraints to the top, right side, and trailing edge of the image view).

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • This is what I have right now actually. Also turns out that having two `>=20` constraints is a red herring. If I remove any one of them, there is still ambiguity. Xcode help (clicking on `(i)` in the warnings) tells me just a `>=` constraint isn't enough to vertically position an element. – Alex B Nov 20 '13 at 05:54
  • @AlexB, the one I have between the image view and the bottom view is = (with priority of 700) not >=. That is enough to satisfy the constraints. – rdelmar Nov 20 '13 at 05:56
  • 1
    So it looks like `>=20` doesn't mean "shrink to 20". You also have to have a `=20` constraint on the same distance, but with a lower priority. – Alex B Nov 20 '13 at 05:56
  • @AlexB, yes, that's true. >=20 could be a million, so that's ambiguous. – rdelmar Nov 20 '13 at 05:57
  • 1
    Cool, looks making the image constraint `=20` **instead** of `>=20` works. Clarify that in the answer and I'll accept it (also not sure where did 30 come from, did you mean 20?) – Alex B Nov 20 '13 at 06:03
  • Thanks for this answer. Logically I would of thought that two >= constraints, with one having lower priority would of resolved ambiguity... guess not! – George B Oct 29 '16 at 04:25
  • Thanks for this answer, it solved our similar issue. I still don't understand why in the case when the label is taller than the image view the constraints aren't ambiguous. The =20 constraint with priority 700 between the image view and bottom view breaks in this case, so all you have is the >=20 constraint. >=20 could be any number between 20 and infinity, so how does it decide what number to use? – Liron Yahdav Dec 24 '16 at 01:11