Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Last active April 27, 2018 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedeexe/9baa1a26987b0e493c472f12cb91b108 to your computer and use it in GitHub Desktop.
Save dedeexe/9baa1a26987b0e493c472f12cb91b108 to your computer and use it in GitHub Desktop.
Adjust Screen when keyboard shows or hide
extension HellViewController {
func startObservers() {
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(showKeyboard), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
nc.addObserver(self, selector: #selector(hideKeyboard), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func stopObservers() {
let nc = NotificationCenter.default
nc.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
nc.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func showKeyboard(notification:Notification) {
if let frame = notification.userInfo?["UIKeyboardBoundsUserInfoKey"] as? NSValue {
let finalFrame = frame.cgRectValue
let offset = finalFrame.height
let chatViewOffset = chatView.bounds.height
bottomSpace.constant = offset
tableview.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: offset + chatViewOffset, right: 0)
view.setNeedsUpdateConstraints()
UIView.animate(withDuration: 0.2, animations: {[unowned self] in
self.view.layoutIfNeeded()
})
}
}
func hideKeyboard(notification:Notification) {
bottomSpace.constant = 0.0
view.setNeedsUpdateConstraints()
resetTableViewInsets()
UIView.animate(withDuration: 0.2, animations: {[unowned self] in
self.view.layoutIfNeeded()
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment