Skip to content

Instantly share code, notes, and snippets.

@RockinPaul
Last active March 21, 2018 17:50
Show Gist options
  • Save RockinPaul/6e5e9ef20d3fe8b4ddda38169020c44e to your computer and use it in GitHub Desktop.
Save RockinPaul/6e5e9ef20d3fe8b4ddda38169020c44e to your computer and use it in GitHub Desktop.
Move view with keyboard. Swift 3/4 version.
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWillShow),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWillHide),
name: NSNotification.Name.UIKeyboardWillHide,
object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y -= keyboardSize.height// 92 or whatever you want
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y += keyboardSize.height// 92 or whatever you want
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment