Last active
March 21, 2018 17:50
-
-
Save RockinPaul/6e5e9ef20d3fe8b4ddda38169020c44e to your computer and use it in GitHub Desktop.
Move view with keyboard. Swift 3/4 version.
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
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