0

I have a label in a View Controller scene inside a root view. I set the constraint of the height to 30 using the Pins button right below. Everything looks pretty in the storyboard.

But when I want to get the size of the element inside my code I always get the value 21. I tried refLabel.bounds.size.height and refLabel.frame.height. I call this in the override func viewDidLoad(){ super.viewDidLoad() function of the corresponding class.

Has this something to do with the intrinsic content size?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
JanScott
  • 265
  • 1
  • 5
  • 14
  • i use Xcode 7 as a IDE – JanScott Dec 18 '15 at 12:24
  • I added a button, to dynamically print the size and what a wonder it works! So why doesn't it work inside the 'viewDidLoad()'-function? – JanScott Dec 18 '15 at 12:42
  • 1
    Frames can be incorrect in viewDidLoad since the subviews layout has not be performed yet. Try in `viewDidLayoutSubviews` More information on : http://stackoverflow.com/questions/14060002/wrong-frame-size-in-viewdidload – Michaël Azevedo Dec 18 '15 at 15:29

1 Answers1

1

Try this code

refLabel.layoutIfNeeded()
let height = refLabel.frame.size.height
print("right height is \(height)")
Bios90
  • 801
  • 2
  • 14
  • 26