Skip to content

Instantly share code, notes, and snippets.

@marquavious
Created May 17, 2017 04:55
Show Gist options
  • Save marquavious/e30e63976c4b1bad2f2aee7e426275f2 to your computer and use it in GitHub Desktop.
Save marquavious/e30e63976c4b1bad2f2aee7e426275f2 to your computer and use it in GitHub Desktop.
A simple way to move the screen up when the keyboard is apparent
// Make sure you add the observer.
func addObserverToKeyboard(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y -= keyboardSize.height
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y += keyboardSize.height
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment