Cory
Cory

Reputation: 2312

How to centre 2 views within a container view with Autolayout

I'm using autolayout in IB and I have 2 labels within a container view. Both labels are "Size to Fit Content" since the strings lengths will vary.

I would like the labels separated by a fixed distance and I would like the combination of "Label 1---fixed distance---Label 2" centred horizontal within the containing view.

enter image description here

I tried selecting both labels at once and adding the "Horizontal Center in Container" constraint, but this appears to centre both textfields independently not as a group.

Upvotes: 1

Views: 749

Answers (2)

Vinayak Parmar
Vinayak Parmar

Reputation: 618

A solution to this is considered in stanford university lectures on ios 7.It works beautifully.Attached here that solution. (Here sdfssfg... thing is label1 and efsdfg.... thing is label2)

enter image description here

Upvotes: 0

Austin
Austin

Reputation: 5655

You can do this by placing the labels inside a container view. Each label is flush against the edges of the container, and the container is centered in your custom view.

Container Constraints Label Constraints

Upvotes: 3

Related Questions