Created
July 20, 2020 18:35
-
-
Save Abhishek9634/20d27acf8560feba5a42138a0b907077 to your computer and use it in GitHub Desktop.
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
extension ViewController { | |
private func setupKeyboardNotifications() { | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(keyboardWillShow(_:)), | |
name: UIResponder.keyboardWillShowNotification, | |
object: nil) | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(keyboardWillHide(_:)), | |
name: UIResponder.keyboardWillHideNotification, | |
object: nil) | |
} | |
@objc | |
private func keyboardWillShow(_ sender: Notification) { | |
guard let info = sender.userInfo, | |
let frame = (info[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { | |
return | |
} | |
self.bottomConstraint.constant = frame.size.height | |
UIView.animate(withDuration: 0.3) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
@objc | |
private func keyboardWillHide(_ sender: Notification) { | |
self.bottomConstraint.constant = 0 | |
self.view.layoutIfNeeded() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment