title: How to programmatically add textField to a scroll view
body: <p>I have a button on my storyboard. This button is inside a view and that view is inside a scroll view.</p>
  
  <p>What I am trying to do sounds relatively simple: When the button is pressed, the button moves down a little bit and a textField appears where the button had previously been.</p>
  
  <p>However, what happens is the button animates, but then returns to its original position, instead of staying moved, and the textField appears as it should.</p>
  
  <p>Because the position of the appearing textField is dependent on the position of the button, pressing the button multiple times simply places a bunch of textfields on top of each other.</p>
  
  <p>Here is a picture of what happens:</p>
  
  <p><a href="http://i.stack.imgur.com/eT9kX.png" rel="nofollow">my Screen:</a></p>
  
  <p>Here is the code I am running when the button is pressed:</p>
  
  <p>@IBAction func addIngredientButtonPressed(sender: UIButton) {</p>
  
  <pre><code>contentView.bounds.size.height += addIngredientLabel.bounds.height
  
  scrollView.contentSize.height += addIngredientLabel.bounds.height
  
  
  //Create Text Field and Set Initial Properties
  let newIngredientTextField = UITextField(frame: CGRectMake(self.addIngredientLabel.frame.minX as CGFloat, self.addIngredientLabel.frame.midY as CGFloat, self.addIngredientLabel.bounds.width,self.addIngredientLabel.bounds.height * 0.60))
  
  newIngredientTextField.font = UIFont.systemFontOfSize(15.0)
  newIngredientTextField.placeholder = "ADD INGREDIENT HERE"
  newIngredientTextField.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Left
  
  newIngredientTextField.alpha = 0.0
  
  scrollView.addSubview(newIngredientTextField)
  
  
  
  UIView.animateWithDuration(0.5) { () -&gt; Void in
  
      self.addIngredientLabel.center.y += self.addIngredientLabel.bounds.height
      newIngredientTextField.alpha = 1.0
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Note:** the length is also being added successfully to the screen, but the button is not staying moved. So in the screenshot above you can indeed scroll up and down **</p>
  
  <p>I noticed that if you comment out the top 2 lines of code, the ones that add height to the contentView and the scrollView, you get almost the right behavior. The button moves and stays moved, the textField appears in the right spot, but if you keep pressing, it won't let you add more textFields than the screen size will hold.</p>
  
  <p>Here is a picture of what happens if you comment out the top two lines. The button won't do anything more if you keep pressing it more:</p>
  
  <p><a href="http://i.stack.imgur.com/lz0yT.png" rel="nofollow">screen filled:</a></p>
  
  <p>I found lots of tutorials that tell you how to add more content than can fit into one screen using the storyboard and setting the inferred metrics to free form, but I have not found anyone trying to add elements dynamically like I am.</p>