I have a UITextField
, and I'm trying to change its position when the keyboard comes up. More specifically, I want to move the text field up so that it sits just above the keyboard. My code looks something like this
let textField = myCustomTextField()
override func viewDidLoad() {
//set up textfield here
//constrains blah blah
textField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: -view.bounds.height * 0.05).isActive = true
//more constraints
}
What I want to do next is change that constraint so that it raises up the textfield when the keyboard comes up. My code looks like this:
@objc func keyboardWillShow(notification: NSNotification) {
textField.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor,
constant: -view.bounds.height * 0.05).constant = 200 //200 is a sample number I have a math calculation there
textField.layoutIfNeeded()
}
That doesn't work because referencing that constraint by just using constraint(equalTo: constant:)
doesn't actually return any constraint. Is there any way to reference that constraint without creating a variable for each constraint I want to change and changing its constant?