title: Swift UIStackView: Positioning elements from center programmatically
body: <p>I am trying to get elements inside a UIStackView to position themselves equally from the center. </p>
  
  <p>This is the desired effect I would like: </p>
  
  <p><a href="https://i.stack.imgur.com/dpYfa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dpYfa.png" alt="enter image description here"></a></p>
  
  <p>As you can see, I would like the two text fields to be equally spaced from each other and aligned center within the stack view. </p>
  
  <p>This stack view will have either 1 - 7 textfields that I need lined up. </p>
  
  <p>Here is what is currently coming out:</p>
  
  <p><a href="https://i.stack.imgur.com/whzTE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/whzTE.png" alt="enter image description here"></a></p>
  
  <p>This is how I am setting the Text Fields</p>
  
  <pre><code>let textLabel = UILabel()
  textLabel.backgroundColor = UIColor.red
  textLabel.widthAnchor.constraint(equalToConstant: 40.0).isActive = true
  textLabel.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
  textLabel.text  = "Hi"
  textLabel.textAlignment = .center
  
  let textLabel1 = UILabel()
  textLabel1.backgroundColor = UIColor.red
  textLabel1.widthAnchor.constraint(equalToConstant: 40.0).isActive = true
  textLabel1.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
  textLabel1.text  = "Hi"
  textLabel1.textAlignment = .center
  </code></pre>
  
  <p>I am generating the stack view like so: </p>
  
  <pre><code>//Stack View
  let stackView   = UIStackView()
  stackView.axis  = UILayoutConstraintAxis.horizontal
  stackView.distribution  = UIStackViewDistribution.equalCentering
  stackView.alignment = UIStackViewAlignment.fill
  stackView.spacing   = 16.0
  
  stackView.addArrangedSubview(textLabel)
  stackView.addArrangedSubview(textLabel1)
  stackView.translatesAutoresizingMaskIntoConstraints = false;
  
  self.view.addSubview(stackView)
  
  //Constraints
  
  stackView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 50).isActive = true
  stackView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -50).isActive = true
  stackView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
  </code></pre>
  
  <p>How do I get the elements inside to align center?</p>
  
  <p>I thought it was </p>
  
  <blockquote>
    <p>UIStackViewDistribution.equalCentering</p>
  </blockquote>
  
  <p>However, that didn't do much.</p>