Skip to content

Instantly share code, notes, and snippets.

@JayachandraA
Created September 18, 2018 13:34
Show Gist options
  • Save JayachandraA/78a94b1b4359026c59a0a037e362488d to your computer and use it in GitHub Desktop.
Save JayachandraA/78a94b1b4359026c59a0a037e362488d to your computer and use it in GitHub Desktop.
How to handle the keyboard show and hide animation in iOS
NotificationCenter.default.addObserver(self, selector: #selector(self.handleKeyboard), name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleKeyboard), name: .UIKeyboardWillShow, object: nil)
func handleKeyboard(_ aNotification: Notification?) {
let info = aNotification?.userInfo
let value = info?[UIKeyboardAnimationDurationUserInfoKey] as? NSValue
var duration: TimeInterval = 0
value?.getValue(&duration)
if aNotification?.name == .UIKeyboardWillHide {
//* KEYBOARD HIDE *
//calculate your view frames and handle UI changes
/*
.
.
.
.
.
*/
moveCustomView(false, duration: duration)
}
if aNotification?.name == .UIKeyboardWillShow {
//* KEYBOARD SHOW *
//calculate your view frames and handle UI changes
/*
.
.
.
.
.
*/
moveCustomView(true, duration: duration)
}
}
func moveCustomView(_ move: Bool, duration time: TimeInterval) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment