Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I align a label vertical center in XCode 6

Tags:

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? Image of contstraints

enter image description here

like image 616
botbot Avatar asked Aug 04 '14 22:08

botbot


People also ask

How do I center text vertically in an element?

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.

How do you align horizontally and vertically centered?

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.

How do I set my vertical center?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.


2 Answers

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.

enter image description here

like image 68
modus Avatar answered Oct 01 '22 04:10

modus


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.

like image 36
Justin Moser Avatar answered Oct 01 '22 05:10

Justin Moser