13

View heirachy:

- RootView
  - ContainerView
    - Label1
    - Label2

I want ContainerView always wraps Label1 and Label2,adjust height of ContainerView when Label1 or Label2 contents change.

My constraints:

H: [Label1]-[Label2(==Label1)]
V: [Label1]-(>=0)
V: [Label2]-(>=0)

No exception but got ambiguous layout.

Loveforgeter
  • 133
  • 1
  • 1
  • 5

3 Answers3

9

Think about that,

If you want height to wrap content, give height to "0" with greater condition view.height >= 0

if you want width , to same thing for width... so that view keeps minimum sizing

Ucdemir
  • 2,070
  • 2
  • 21
  • 35
2

Please follow the logic below. It should get you through your problem:

  1. The first thing you need to do is make your Labels wrap their content. You can follow Mark's suggestion to do this here

  2. Save a reference to the height constraint of your ContainerView from your Storyboard or .xib file

  3. Update the height constraint with an appropriate height every time after you update the content of the two labels

This worked for me in a similar situation. You also have to add all the constraints up down left right for the ContainerView to fit in the RootView

Community
  • 1
  • 1
mgm
  • 1,238
  • 13
  • 16
  • I know one solution is that, add a low priority zero height constraint to ContainerView.But I doubt if that is appropriate. – Loveforgeter Nov 30 '15 at 10:31
  • @Loveforgeter That is what worked for me and got rid of the ambiguous layout problem. The only thing is that you need to update the height constraint every time label content is changing so not 100% automated. Please post if you find an alternative though – mgm Nov 30 '15 at 10:57
0

In interface builder:

  1. First set height to constant value like 200 (for ease).
  2. Set all child constraints (with equal relation).
  3. Delete height of parent (that was 200).

This worked for me and no error appears in IB. because height is not ambiguous any more.

For width can do this in same way.

Mahdi Moqadasi
  • 1,609
  • 16
  • 41