Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Created November 26, 2017 13:36
Show Gist options
  • Save sjehutch/4defbdb2123ed899cad91e4b29ff65c8 to your computer and use it in GitHub Desktop.
Save sjehutch/4defbdb2123ed899cad91e4b29ff65c8 to your computer and use it in GitHub Desktop.
Swift 3 move keyboard up textfield
func animateTextField(textField: UITextField, up: Bool)
{
let movementDistance:CGFloat = -130
let movementDuration: Double = 0.3
var movement:CGFloat = 0
if up
{
movement = movementDistance
}
else
{
movement = -movementDistance
}
UIView.beginAnimations("animateTextField", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
UIView.commitAnimations()
}
func textFieldDidBeginEditing(textField: UITextField)
{
self.animateTextField(textField: textField, up:true)
}
func textFieldDidEndEditing(textField: UITextField)
{
self.animateTextField(textField: textField, up:false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment