Skip to content

Instantly share code, notes, and snippets.

@Abhishek9634
Created July 20, 2020 18:35
Show Gist options
  • Save Abhishek9634/20d27acf8560feba5a42138a0b907077 to your computer and use it in GitHub Desktop.
Save Abhishek9634/20d27acf8560feba5a42138a0b907077 to your computer and use it in GitHub Desktop.
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