Search code examples
iosswiftautolayout

How to programmatically place two labels in center side by side with spacing using auto layout in ios swift?


I am very new to swift and IOS and I want to place two labels side by side horizontally center programmatically. Below is my code

let secondLabel : UILabel = {
    let label = UILabel()
    label.text = "1234"
    label.textAlignment = .center
    label.translatesAutoresizingMaskIntoConstraints = false
    label.backgroundColor = UIColor.blue
    return label
}()

let thirdLabel : UILabel = {
    let label = UILabel()
    label.text = "5678"
    label.textAlignment = .center
    label.translatesAutoresizingMaskIntoConstraints = false
    label.backgroundColor = UIColor.red
    return label
}()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    setupLayout()
}

private func setupLayout(){
    view.addSubview(secondLabel)
    //secondLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    secondLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    secondLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
    secondLabel.heightAnchor.constraint(equalToConstant: 100).isActive = true

    view.addSubview(thirdLabel)
    //thirdLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    thirdLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    thirdLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
    thirdLabel.heightAnchor.constraint(equalToConstant: 100).isActive = true

    self.view.addConstraint(NSLayoutConstraint(
        item: thirdLabel,
        attribute: .left,
        relatedBy: .equal,
        toItem: secondLabel,
        attribute: .right,
        multiplier: 1.0,
        constant: 10
        ))
}

But this is how it looks now

enter image description here

Please help me to move the labels to the center


Solution

  • You can use UIStackView to solve your problem. Please update the following code in your setupLayout method.

     private func setupLayout(){
            let stackview = UIStackView()
            stackview.axis = .horizontal
            stackview.spacing = 10
            stackview.translatesAutoresizingMaskIntoConstraints = false
            stackview.addArrangedSubview(secondLabel)
            secondLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
            secondLabel.heightAnchor.constraint(equalToConstant: 100).isActive = true
    
            stackview.addArrangedSubview(thirdLabel)
            thirdLabel.widthAnchor.constraint(equalToConstant: 100).isActive = true
            thirdLabel.heightAnchor.constraint(equalToConstant: 100).isActive = true
    
            self.view.addSubview(stackview)
            stackview.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
            stackview.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        }
    

    Output:- enter image description here