Last active
April 27, 2018 14:10
-
-
Save dedeexe/9baa1a26987b0e493c472f12cb91b108 to your computer and use it in GitHub Desktop.
Adjust Screen when keyboard shows or hide
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 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