j0h4nn3s
j0h4nn3s

Reputation: 2076

iOS Constraints: how to give UILabel height constraint priorities

I have following problem: I have a view with fixed height. In this view, there are two singleline UILables. The view is bigger than both of the labels so I want them to be displayed like this: UIView with two UILabels

I added three constraints to achieve this:

(if you want to, i can add the code for this but I think its quite obvious)

So now the problem is following. Sometimes the view renders as intended, but somtimes it renders differently: wrong UILabels

The behaviour is quite random. I think it is because, both UILabels have a (autogenerated) height constraints with the same priority. And sometimes, the first label gets the correct size, sometimes the second label gets it. So is there a way to change the priority of those autogenerated height constraints or another way to achieve this layout? (I don't want to use a containerview for the second UILabel)

best regards

Upvotes: 1

Views: 2979

Answers (1)

Wain
Wain

Reputation: 119031

Your constraints are ambiguous, hence the random result. You haven't specified how the height should be used.

You should specify this by setting the vertical hugging priority of the top label to a value higher than the vertical hugging of the bottom label.

Upvotes: 8

Related Questions