How to center 2 vertically stacked UILabels vertically

I have 2 UILabels that are arranged vertically:

+----------------------------+
|  +----------------------+  |
|  | blah blah blah       |  |
|  +----------------------+  |
|                            |
|  +----------------------+  |
|  | asdf fdsa            |  |
|  | asdfear aoeirhaleir  |  |
|  +----------------------+  |
+----------------------------+

In UIViews, their text is loaded dynamically at run time, so they can have 1 to 3 lines of text. I want to align two vertically after loading the text. They should be vertically centered along their "center of mass."

How can i do this?

Edit: like this: enter image description here

+3
source share
3 answers

Place the two labels in the UIView container. Limit the edges of the labels to the edges of the container (except for the bottom of the top label and the top of the bottom label, which should be bounded by each other), and then vertically center the container at its top level.

:

UIView rootView
  UIView containerView
    UILabel topLabel
    UILabel bottomLabel

:

|[topLabel]|
|[bottomLabel]|
V:|[topLabel][bottomLabel]|
|-[containerView]-|
[NSLayoutConstraint constraintWithFirstItem:containerView firstAttribute:NSLayoutConstraintAttributeCenterY relation:NSLayoutRelationEquals secondItem:rootView secondAttribute:NSLayoutConstraintAttributeCenterY multiplier:1 constant:0]

( IB, , IB StackOverflow.)

. iOS , , .

+4

IB. . . , , . , , , , .

, , .

+1

- NSLayoutContraint, Visual Format Language (VFL) Xcode/IB - , , X- X- .

Apple Auto Layout Guide .

0
source

All Articles