title: Adding labels programmatically, aligned with labels from Storyboard
body: <p>Desired View:</p>
  
  <p><a href="https://i.stack.imgur.com/dIV7P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dIV7P.png" alt="enter image description here"></a></p>
  
  <p>In Storyboard I have two labels with the blue background that I am creating in Autolayout.  Their position will never change.  Next, I would like to add anywhere from 1 to 10 labels in code in <code>cellForRowAtIndexPath</code> below the blue background labels.  </p>
  
  <p>I am struggling to align the the labels added in code (brown background) with the ones created in Autolayout (blue background).</p>
  
  <p>Below is my failed attempt:</p>
  
  <p><a href="https://i.stack.imgur.com/lDfRw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lDfRw.png" alt="enter image description here"></a></p>
  
  <p>Two approaches that failed: </p>
  
  <ol>
  <li><p>In <code>cellForRowAtIndexPath</code> get the frame of "B AutoLayout Static Label" and use the X position for Dynamic Labels.  Did not work.</p></li>
  <li><p>Adding constraints also did not work -- perhaps I am not adding the constraints correctly.</p></li>
  </ol>
  
  <p>Here is the code:</p>
  
  <pre><code>class TableViewController: UITableViewController {
  
      var cellHeight = [Int: CGFloat]()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          tableView.estimatedRowHeight = 85.0
          self.tableView.rowHeight = UITableViewAutomaticDimension
      }
  
      override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          return 1
      }
  
      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return 4
      }
  
  
      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
  
  
          let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell
          let xLocation = cell.labelBStatic.frame.origin.x
          var yLocation = cell.labelBStatic.frame.origin.y
          let height = cell.labelBStatic.frame.size.height
  
          var startYLocation = yLocation + height + 20
          var i = 0
          if indexPath.row % 2 == 0 {
              i = 5
          } else {
              i = 7
          }
  
          while i &lt; 10 {
              let aLabel = UILabel()
              aLabel.backgroundColor = UIColor.orangeColor()
              aLabel.text = "Label # \(i)"
              cell.contentView.addSubview(aLabel)
              addConstraints(aLabel, verticalSpacing: startYLocation)
              startYLocation += 20
              i++
          }
  
          print(startYLocation)
          cellHeight[indexPath.row] = startYLocation
          return cell
      }
  
      func addConstraints(labelView: UILabel, verticalSpacing: CGFloat) {
          // set Autoresizing Mask to false
          labelView.translatesAutoresizingMaskIntoConstraints = false
  
          //make dictionary for views
          let viewsDictionary = ["view1": labelView]
  
          //sizing constraints
          let view1_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[view1(&gt;=50)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
  
          labelView.addConstraints(view1_constraint_H)
  
          //position constraints
          let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[view1]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
          let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-\(verticalSpacing)-[view1]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: viewsDictionary)
  
          view.addConstraints(view_constraint_H as! [NSLayoutConstraint])
          view.addConstraints(view_constraint_V as! [NSLayoutConstraint])
  
      }
  
          override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -&gt; CGFloat {
  
          if let height = cellHeight[indexPath.row] {
              return height
          }
          return 0
      }
  </code></pre>
  
  <p>Below is the <code>Storyboard</code> setup (Both labels are centered horizontally):</p>
  
  <p><a href="https://i.stack.imgur.com/7Eh6k.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Eh6k.png" alt="enter image description here"></a></p>
  
  <p>Question: How can I get my dynamic labels that I am creating in <code>cellForRowAtIndexPath</code> left align with my static labels that were created in <code>Storyboard</code> to match my desired view on top?</p>