Reputation: 2117
I want to align set of labels next to each other using AutoLayout constraint programmatically.
NOTE:And each label must have different width.Like i need to assign in array [50,40,70,30,20,10]
I find some relevant questions but not useful in my case
for var i=0;i<7;i++{
let DataLabel = UILabel()
DataLabel.translatesAutoresizingMaskIntoConstraints = false
DataLabel.text = NamingLabel[i]
DataLabel.backgroundColor = UIColor.lightGrayColor()
Self.view.addSubview(DataLabel)
}
If the constraint is in Visual Format language means will be more useful.
Thanks in Advance.
Upvotes: 1
Views: 1517
Reputation: 27620
You need the following VFL strings for you labels:
"V:|-40-[*labelName*]"
. Change the 40
to whatever suits you best. "H:|-[label0(50)][label1(40)][label2(70)][label3(30)][label4(20)][label5(10)][label6(30)]"
To be able to use Visual Format Language you have create a dictionary that contains all labels. The values of that dictionary are the UILabels
and the keys are Strings
that you use in the VFL strings to define which label a constraint should be added to. In this case I use the keys "label0", "label1", "label2" etc.
Here is an example how you could add the constraints in the loop:
let labelWidths = [50, 40, 70, 30, 20, 10, 30]
var labels = [String: UILabel]()
var allConstraints = [NSLayoutConstraint]()
var horizontalConstraintsString = "H:|-"
for i in 0..<7 {
let labelName = "label\(i)"
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
// set text, textColor etc.
view.addSubview(label)
labels[labelName] = label
// add vertical constraints to label
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[\(labelName)]", options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(verticalConstraints)
// add label to horizontal VFL string
horizontalConstraintsString += "[\(labelName)(\(labelWidths[i]))]"
}
// get horizontal contraints from horizontal VFL string
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(horizontalConstraintsString, options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(horizontalConstraints)
NSLayoutConstraint.activateConstraints(allConstraints)
UPDATE:
As you you are writing in your comment that it still does not work for you, here is the full ViewController
class from my example project. So just create a new project in Xcode and paste the following code into the ViewController
class:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.lightGrayColor()
let labelWidths = [50, 40, 70, 30, 20, 10, 30]
var labels = [String: UILabel]()
var allConstraints = [NSLayoutConstraint]()
var horizontalConstraintsString = "H:|-"
for i in 0..<7 {
let labelName = "label\(i)"
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "\(i)"
label.layer.cornerRadius = 6
label.clipsToBounds = true
label.backgroundColor = UIColor.grayColor()
view.addSubview(label)
labels[labelName] = label
// add vertical constraints to label
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[\(labelName)]", options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(verticalConstraints)
// add label to horizontal VFL string
horizontalConstraintsString += "[\(labelName)(\(labelWidths[i]))]"
}
// get horizontal contraints from horizontal VFL string
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat(horizontalConstraintsString, options: [], metrics: nil, views: labels)
allConstraints.appendContentsOf(horizontalConstraints)
NSLayoutConstraint.activateConstraints(allConstraints)
}
}
I added a background color and a corner radius to the labels so you can see where one label ends and the next begins. When you run this project you should see the following:
Upvotes: 3