title: Center aligning multiple labels using Swift 3 Xcode 8
body: <p>I want to align three labels as a group to center Horizontally. Two of the labels have variable size. I referred to following post thoroughly but my app is crashing with following message:</p>
  
  <p><strong>POST:</strong>
  <a href="https://stackoverflow.com/questions/29184492/center-align-labels-and-images-inside-uiview-programatically-with-swift">Center align Labels and images inside UIView programatically with Swift</a></p>
  
  <p><strong>Error Message:</strong></p>
  
  <blockquote>
    <p>When added to a view, the constraint's items must be descendants of
    that view (or the view itself). This will crash if the constraint
    needs to be resolved before the view hierarchy is assembled.</p>
  </blockquote>
  
  <p>Couple of things:
  I already have constraints in Autolayout for these three labels. For "InLabel" I have leading space from margin, width and spacing from top neighbor. For other two labels (durationLable &amp; timeUnitLabel) I have space inbetween all three labels and spacing from top neighbor.</p>
  
  <p>UIView is initialized as indurtimeView = UIView() in the beginning part of class ViewController</p>
  
  <p>I'm calling function indurtimeLabelcenter() in "viewDidLoad" function after calling super.viewDidLoad() in ViewController class.</p>
  
  <p>Here is my code for function indurtimeLabelcenter():</p>
  
  <pre><code>func indurtimeLabelcenter() {
      //Align "In", "Duration Lable" and "Time Unit Label" to center
      self.view.addSubview(indurtimeView)
      indurtimeView.translatesAutoresizingMaskIntoConstraints = false
      indurtimeView.clipsToBounds = true
      indurtimeView.addSubview(InLabel)
  
      //Constraints
      var viewsDict = Dictionary &lt;String, UIView&gt;()
      viewsDict["InLabel"] = InLabel
      viewsDict["durationLabel"] = durationLabel
      viewsDict["timeunitLabel"] = timeUnitLabel
  
      indurtimeView.addConstraints(
          NSLayoutConstraint.constraints(
              withVisualFormat: "V:|[InLabel]|", options: [], metrics: nil, views: viewsDict))
      indurtimeView.addConstraints(
          NSLayoutConstraint.constraints(
              withVisualFormat: "V:|[durationLabel]|", options: [], metrics: nil, views: viewsDict))
      indurtimeView.addConstraints(
          NSLayoutConstraint.constraints(
              withVisualFormat: "V:|[timeunitLabell]|", options: [], metrics: nil, views: viewsDict))
      indurtimeView.addConstraints(
          NSLayoutConstraint.constraints(
              withVisualFormat: "H:|-[InLabel]-5-[durationLabel]-5-[timeunitLabel]-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: viewsDict))
      //costView!.backgroundColor = UIColor.redColor()
  
      // center costView inside self
      let centerXCons = NSLayoutConstraint(item: indurtimeView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0);
      let centerYCons = NSLayoutConstraint(item: indurtimeView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0);
      self.view.addConstraints([centerXCons, centerYCons])
  }
  </code></pre>
  
  <p>First time writing App in Swift so bear with my ignorance.</p>