I'm at a loss trying to understand a positioning problem with UITextField
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.
I've set up a very simple view controller that reproduces the problem:
The view controller class is totally basic as well:
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
}
}
Nothing fancy. When I launch it, I get the expected screen with the text areas moved into the blue zone:
If I type some text into the UITextView
(the green one), nothing happens:
But as soon as I enter text into the UITextField
(the red one), both text areas are moved back to their original position:
The same behaviour occurs on various iPhone simulators and on my own iPhone, so it's consistent.
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 UITextView
area? I'm probably going to be able to avoid the problem by using UITextView
s instead of UITextField
s, but I still don't know what's happening :-|
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 :-)