I have searched pretty much everywhere and can't find a solution for this. So i have a log in screen and when the keyboard shows the textfields move up for readability. However as soon as I type or switch to a different textfield the fields jump back to their original position...
Here is the code
override func viewDidLoad() {
super.viewDidLoad()
usernameField.delegate = self
passwordField.delegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.animateTextField(textField: textField, up:true)
}
func textFieldDidEndEditing(_ textField: UITextField) {
self.animateTextField(textField: textField, up:false)
}
func animateTextField(textField: UITextField, up: Bool)
{
let movementDuration: Double = 0.3
var labelMove: CGFloat = -20
var userMove: CGFloat = -40
var passMove: CGFloat = -50
if up
{
labelMove = -20
userMove = -40
passMove = -50
}
else
{
labelMove = 20
userMove = 40
passMove = 50
}
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.titleLabel.frame = self.titleLabel.frame.offsetBy(dx: 0, dy: labelMove)
self.usernameField.frame = self.usernameField.frame.offsetBy(dx: 0, dy: userMove)
self.passwordField.frame = self.passwordField.frame.offsetBy(dx: 0, dy: passMove)
UIView.commitAnimations()
}
Please help?