Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center 2 labels horizontally side by side in XCode 6?

This is a result I need:

enter image description here

Width of labels are not fixed. They may be equal, first may be larger then second, and the second may be larger than first. I have found some solutions in other questions, but it does not satisfy me, because I do not want to add extra views and my widths are not fixed.

like image 357
Bartłomiej Semańczyk Avatar asked Apr 24 '15 11:04

Bartłomiej Semańczyk


2 Answers

I made some research, and it is still impossible without extra view:-)

But the simplest way is:

1. constraints for wrapper:

enter image description here

2. constraints for embedded views:

enter image description hereenter image description here

3. The result is following:

enter image description here

enter image description here

like image 107
Bartłomiej Semańczyk Avatar answered Sep 28 '22 14:09

Bartłomiej Semańczyk


Heres how to do it programmatically

let margins = view.safeAreaLayoutGuide
//Let x equal whatever you want your height to be 
//Let y equal whatever you want your width to be 

view1.topAnchor.constraint(equalTo: margins.topAnchor).isActive = true
view1.trailingAnchor.constraint(equalTo: margins.centerXAnchor).isActive = true
view1.widthAnchor.constraint(equalToConstant: X).isActive = true
view1.heightAnchor.constraint(equalToConstant: Y).isActive = true

view2.leadingAnchor.constraint(equalTo: margins.centerXAnchor, constant: 8).isActive = true
view2.topAnchor.constraint(equalTo: view2.topAnchor).isActive = true
view2.bottomAnchor.constraint(equalTo: view2.bottomAnchor).isActive = true
view2.widthAnchor.constraint(equalToConstant: X).isActive = true
like image 43
tommmm Avatar answered Sep 28 '22 14:09

tommmm