Reputation: 29316
I was reading this post for advice on how to vertically align text within a UILabel, and the top comment seemed to present a great solution.
I have my UILabel
,with number of lines to 0. Then in viewDidLoad
for the View Controller it's a part of, I called sizeToFit
on it, but it still only occupies the middle.
It looks like this in Interface Builder:
And looks the exact same when I run it, the text is still really far away from the navigation bar when I'd prefer it to be very close, preferably at the top of that outline box.
What am I doing wrong?
Upvotes: 0
Views: 1101
Reputation: 119242
That screenshot shows a specific height constraint against the label (the blue I-bar to the right). This will override any sizeToFit calls, since they just affect the intrinsic size.
If you delete that constraint, the label's intrinsic size will leave it the minimum size possible to contain the text, and will therefore be up next to the navigation bar.
Upvotes: 0
Reputation: 3761
Right at the bottom just above his original answer he quotes another user saying
If your label is included in a nib or storyboard as a subview of the view of a ViewController that uses autolayout, then putting your sizeToFit call into viewDidLoad won't work, because autolayout sizes and positions the subviews after viewDidLoad is called and will immediately undo the effects of your sizeToFit call. However, calling sizeToFit from within viewDidLayoutSubviews will work.
Upvotes: 1