Guest User

Untitled

a guest
Jan 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. // MARK: - UIKeyBoard
  2.  
  3. extension Your_Controller {
  4.  
  5. func registerKeyboardNotifications() {
  6. NotificationCenter.default.addObserver(self,
  7. selector: #selector(keyboardWillShow(notification:)),
  8. name: NSNotification.Name.UIKeyboardWillShow,
  9. object: nil)
  10. NotificationCenter.default.addObserver(self,
  11. selector: #selector(keyboardWillHide(notification:)),
  12. name: NSNotification.Name.UIKeyboardWillHide,
  13. object: nil)
  14. }
  15.  
  16. override func viewWillDisappear(_ animated: Bool) {
  17. super.viewWillDisappear(animated)
  18. NotificationCenter.default.removeObserver(self)
  19. }
  20.  
  21. @objc func keyboardWillShow(notification: NSNotification) {
  22. let userInfo: NSDictionary = notification.userInfo! as NSDictionary
  23. let keyboardInfo = userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue
  24. let keyboardSize = keyboardInfo.cgRectValue.size
  25. let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height + 30, right: 0)
  26. UI?.scrollView.contentInset = contentInsets
  27. UI?.scrollView.scrollIndicatorInsets = contentInsets
  28. }
  29.  
  30. @objc func keyboardWillHide(notification: NSNotification) {
  31. UI?.scrollView.contentInset = .zero
  32. UI?.scrollView.scrollIndicatorInsets = .zero
  33. }
  34. }
Add Comment
Please, Sign In to add comment