Reputation: 2151
I got a UIView whose child is a UILabel. I then aligned the left, top, right, and bottom of the UILabel to that of its superview (the UIView) using constraints.
But what I'm getting is a UILabel that matches the height of its superview but not its intrinsic height (smaller or larger than its text content depending on the size of the UIView). What I expect is for the UIView to resize itself to fit exactly the height of the UILabel.
So how do I do this using only the interface builder?
Upvotes: 0
Views: 1815
Reputation: 513
In my case, there were two UILabel inside a UIView. One of the UILabel was vertically centered on the UIview. Another (ie, second one) UILabel was given top and bottom constraint according to UIView. My target was to resize UIView according to the second UILabel. UIView and second UILabel both were taking weird heights. So I set the vertical content hugging priority of the second UILabel from 251 to 1000.
Long answer short, I had to set content hugging priority 1000
Upvotes: 0
Reputation: 6557
If someone is experiencing this issue, while:
In my case, I set Vertical Content Hugging Priority
of BOTH UILabels to 1000. Then one of the labels had a different font. (ie. one label was supposed to be smaller than the other one)
The height was being ambiguous, because both labels were trying to force their height on the superview
. Once I lowered the priority on the smaller label, everything worked fine.
Upvotes: 0
Reputation: 288
It's super easy using auto-layout. Just, follow these steps
1 - Drag a UIView and align it vertically and horizontally
in centre.
2 - Now drag UILabel into the UIView and align the label also horizontally and vertically in centre
.Now, the IB aligns the label at the centre WRT to the superview and not the UIView.So, change that in the size-inspector
section.
3 - Once you have done that and all red lines are removed, select both the label and the UIView together using the command key.
4 - Now, give them constraints as follows
leading = 0, trailing = 0
and select the equal width
and equal height
.
There you are done.If you wanna test that the UIView size is respective to the label's content, try increasing the label's font to a bigger size and you will see that the size of the view will be the same as the size of the label.
There , you are done :-D. Hope, this was helpful.
Upvotes: 0
Reputation: 2043
Align top
, trailing
, bottom
and leading
of UILabel
with UIView
(add constraints).
Remove height
and bottom
constraints from UIView if any.
If the content is large, you probably need to wrap UILabel
into a UIScrollView
in place of UIView
. and add a bottom
constraint to UIScrollView
Content Hugging Priority
of UILabel
(251) is more than that of UIView
(250) by default. Verify this
Upvotes: 1
Reputation: 77661
In order to make the UILabel keep its size and force the outer view to resize you can update the values for Content Hugging Priority
to 1000
(a.k.a. Required). This can be done in the measurements
panel when selecting the label in Interface Builder.
I think they are set as a default to 750
.
This should (if there is nothing else causing the change) make the label take its intrinsic content size and force its superview to conform to that size also.
Upvotes: 1