title: UITextField position reset when entering text
body: <p>I'm at a loss trying to understand a positioning problem with <code>UITextField</code>s on iOS. I've designed a simple user interface using Interface Builder, with a picture that takes up the top half of the screen, and then some text fields underneath where the user needs to enter some information. Everything is constrained using auto-layout and displays nicely. However, when the user starts editing a text field, either the keyboard moves up and hides the field, or the whole screen moves up and the picture gets hidden, none of which is acceptable, so my approach has been to move the text field to the top of the screen when the user starts editing it, and move it back to its former place when the editing is done. The moving up and down works fine. However, whenever I actually type any text in the field, it gets reset to its original position – actually, all views in the superview get reset to their original positions. I've tried removing constraints, or setting the vertical constraints to be removed at build time, nothing changes. </p>
  
  <p>I've set up a very simple view controller that reproduces the problem: 
  <a href="https://i.stack.imgur.com/JGRtm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JGRtm.png" alt="Simple view controller in Interface builder"></a></p>
  
  <p>The view controller class is totally basic as well: </p>
  
  <pre><code>import UIKit
  class ViewController: UIViewController {
      @IBOutlet weak var textField: UITextField!
      @IBOutlet weak var textView: UITextView!
      override func viewDidLoad() {
          super.viewDidLoad()
      }
      override func viewDidAppear(_ animated: Bool) {
          textField.frame.origin.y = 100
          textView.frame.origin.y = 150
      }
  }
  </code></pre>
  
  <p>Nothing fancy. When I launch it, I get the expected screen with the text areas moved into the blue zone: </p>
  
  <p><a href="https://i.stack.imgur.com/M0DEo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M0DEo.png" alt="original screen"></a></p>
  
  <p>If I type some text into the <code>UITextView</code> (the green one), nothing happens: </p>
  
  <p><a href="https://i.stack.imgur.com/gA5Pa.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gA5Pa.png" alt="entering text in the UITextView"></a></p>
  
  <p>But as soon as I enter text into the <code>UITextField</code> (the red one), both text areas are moved back to their original position: </p>
  
  <p><a href="https://i.stack.imgur.com/t2ya4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t2ya4.png" alt="entering text in the UITextField"></a></p>
  
  <p>The same behaviour occurs on various iPhone simulators and on my own iPhone, so it's consistent. </p>
  
  <p>I'm expecting that the whole view controller is getting some signal to redraw everything the way it was, but why? And why doesn't it happen when I enter text in the <code>UITextView</code> area? I'm probably going to be able to avoid the problem by using <code>UITextView</code>s instead of <code>UITextField</code>s, but I still don't know what's happening :-|</p>
  
  <p>EDIT: I get it, I didn't realize that the constraints are something to play around with ;-). I ended up simply recreating the constraints with the proper ordering depending on what field is being brought to the top. All the answers helped though! Thanks a lot :-)</p>