Created
May 17, 2017 04:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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