MessagesViewController+Keyboard.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2018 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import Foundation
  21. extension MessagesViewController {
  22. // MARK: - Register / Unregister Observers
  23. internal func addKeyboardObservers() {
  24. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleKeyboardDidChangeState(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  25. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleTextViewDidBeginEditing(_:)), name: UITextView.textDidBeginEditingNotification, object: nil)
  26. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.adjustScrollViewInset), name: UIDevice.orientationDidChangeNotification, object: nil)
  27. }
  28. internal func removeKeyboardObservers() {
  29. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  30. NotificationCenter.default.removeObserver(self, name: UITextView.textDidBeginEditingNotification, object: nil)
  31. NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
  32. }
  33. // MARK: - Notification Handlers
  34. @objc
  35. private func handleTextViewDidBeginEditing(_ notification: Notification) {
  36. if scrollsToBottomOnKeybordBeginsEditing {
  37. guard let inputTextView = notification.object as? InputTextView, inputTextView === messageInputBar.inputTextView else { return }
  38. messagesCollectionView.scrollToBottom(animated: true)
  39. }
  40. }
  41. @objc
  42. private func handleKeyboardDidChangeState(_ notification: Notification) {
  43. guard let keyboardEndFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
  44. guard !isMessagesControllerBeingDismissed else { return }
  45. let newBottomInset = view.frame.height - keyboardEndFrame.minY - iPhoneXBottomInset
  46. let differenceOfBottomInset = newBottomInset - messageCollectionViewBottomInset
  47. if maintainPositionOnKeyboardFrameChanged && differenceOfBottomInset != 0 {
  48. let contentOffset = CGPoint(x: messagesCollectionView.contentOffset.x, y: messagesCollectionView.contentOffset.y + differenceOfBottomInset)
  49. messagesCollectionView.setContentOffset(contentOffset, animated: false)
  50. }
  51. messageCollectionViewBottomInset = newBottomInset
  52. }
  53. @objc
  54. internal func adjustScrollViewInset() {
  55. if #available(iOS 11.0, *) {
  56. // No need to add to the top contentInset
  57. } else {
  58. let navigationBarInset = navigationController?.navigationBar.frame.height ?? 0
  59. let statusBarInset: CGFloat = UIApplication.shared.isStatusBarHidden ? 0 : 20
  60. let topInset = navigationBarInset + statusBarInset
  61. messagesCollectionView.contentInset.top = topInset
  62. messagesCollectionView.scrollIndicatorInsets.top = topInset
  63. }
  64. }
  65. // MARK: - Helpers
  66. internal var keyboardOffsetFrame: CGRect {
  67. guard let inputFrame = inputAccessoryView?.frame else { return .zero }
  68. return CGRect(origin: inputFrame.origin, size: CGSize(width: inputFrame.width, height: inputFrame.height - iPhoneXBottomInset))
  69. }
  70. /// On the iPhone X the inputAccessoryView is anchored to the layoutMarginesGuide.bottom anchor
  71. /// so the frame of the inputAccessoryView is larger than the required offset
  72. /// for the MessagesCollectionView.
  73. ///
  74. /// - Returns: The safeAreaInsets.bottom if its an iPhoneX, else 0
  75. private var iPhoneXBottomInset: CGFloat {
  76. if #available(iOS 11.0, *) {
  77. guard UIScreen.main.nativeBounds.height == 2436 else { return 0 }
  78. return view.safeAreaInsets.bottom
  79. }
  80. return 0
  81. }
  82. }