Whenever I add constraints to the height and width of my UILabel, the text will then jump to the top of the controller.
How can I vertically center the UILabel in the ViewController?
To vertically center text within an element, you can also use the CSS line-height property. You'll have to set the property with a value that is equal to the container element's height.
For vertical alignment, set the parent element's width / height to 100% and add display: table . Then for the child element, change the display to table-cell and add vertical-align: middle . For horizontal centering, you could either add text-align: center to center the text and any other inline children elements.
To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.
click on Align button on bottom toolbar in IB then select Horizontal or Vertical center. First you might want to fix width and height as well, if you want to preserve size.
For an explanation of why this happens:
Whenever you add constraints to a view in interface builder you are telling IB that you want to opt in to auto layout and now you have to take full control of the layout. The reason you see the label at the top of the superview is because you haven't added any constraints that determine the label's location within the superview. See mohacs answer on how to add the constraints.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With