MessagesViewController+Keyboard.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2019 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. import InputBarAccessoryView
  22. internal extension MessagesViewController {
  23. // MARK: - Register / Unregister Observers
  24. internal func addKeyboardObservers() {
  25. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleKeyboardDidChangeState(_:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  26. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.handleTextViewDidBeginEditing(_:)), name: UITextView.textDidBeginEditingNotification, object: nil)
  27. NotificationCenter.default.addObserver(self, selector: #selector(MessagesViewController.adjustScrollViewTopInset), name: UIDevice.orientationDidChangeNotification, object: nil)
  28. }
  29. internal func removeKeyboardObservers() {
  30. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
  31. NotificationCenter.default.removeObserver(self, name: UITextView.textDidBeginEditingNotification, object: nil)
  32. NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
  33. }
  34. // MARK: - Notification Handlers
  35. @objc
  36. private func handleTextViewDidBeginEditing(_ notification: Notification) {
  37. if scrollsToBottomOnKeyboardBeginsEditing {
  38. guard let inputTextView = notification.object as? InputTextView, inputTextView === messageInputBar.inputTextView else { return }
  39. messagesCollectionView.scrollToBottom(animated: true)
  40. }
  41. }
  42. @objc
  43. private func handleKeyboardDidChangeState(_ notification: Notification) {
  44. guard !isMessagesControllerBeingDismissed else { return }
  45. guard let keyboardStartFrameInScreenCoords = notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect else { return }
  46. guard !keyboardStartFrameInScreenCoords.isEmpty || UIDevice.current.userInterfaceIdiom != .pad else {
  47. // WORKAROUND for what seems to be a bug in iPad's keyboard handling in iOS 11: we receive an extra spurious frame change
  48. // notification when undocking the keyboard, with a zero starting frame and an incorrect end frame. The workaround is to
  49. // ignore this notification.
  50. return
  51. }
  52. // Note that the check above does not exclude all notifications from an undocked keyboard, only the weird ones.
  53. //
  54. // We've tried following Apple's recommended approach of tracking UIKeyboardWillShow / UIKeyboardDidHide and ignoring frame
  55. // change notifications while the keyboard is hidden or undocked (undocked keyboard is considered hidden by those events).
  56. // Unfortunately, we do care about the difference between hidden and undocked, because we have an input bar which is at the
  57. // bottom when the keyboard is hidden, and is tied to the keyboard when it's undocked.
  58. //
  59. // If we follow what Apple recommends and ignore notifications while the keyboard is hidden/undocked, we get an extra inset
  60. // at the bottom when the undocked keyboard is visible (the inset that tries to compensate for the missing input bar).
  61. // (Alternatives like setting newBottomInset to 0 or to the height of the input bar don't work either.)
  62. //
  63. // We could make it work by adding extra checks for the state of the keyboard and compensating accordingly, but it seems easier
  64. // to simply check whether the current keyboard frame, whatever it is (even when undocked), covers the bottom of the collection
  65. // view.
  66. guard let keyboardEndFrameInScreenCoords = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return }
  67. let keyboardEndFrame = view.convert(keyboardEndFrameInScreenCoords, from: view.window)
  68. let newBottomInset = requiredScrollViewBottomInset(forKeyboardFrame: keyboardEndFrame)
  69. let differenceOfBottomInset = newBottomInset - messageCollectionViewBottomInset
  70. if maintainPositionOnKeyboardFrameChanged && differenceOfBottomInset != 0 {
  71. let contentOffset = CGPoint(x: messagesCollectionView.contentOffset.x, y: messagesCollectionView.contentOffset.y + differenceOfBottomInset)
  72. messagesCollectionView.setContentOffset(contentOffset, animated: false)
  73. }
  74. messageCollectionViewBottomInset = newBottomInset
  75. }
  76. // MARK: - Inset Computation
  77. @objc
  78. internal func adjustScrollViewTopInset() {
  79. if #available(iOS 11.0, *) {
  80. // No need to add to the top contentInset
  81. } else {
  82. let navigationBarInset = navigationController?.navigationBar.frame.height ?? 0
  83. let statusBarInset: CGFloat = UIApplication.shared.isStatusBarHidden ? 0 : 20
  84. let topInset = navigationBarInset + statusBarInset
  85. messagesCollectionView.contentInset.top = topInset
  86. messagesCollectionView.scrollIndicatorInsets.top = topInset
  87. }
  88. }
  89. private func requiredScrollViewBottomInset(forKeyboardFrame keyboardFrame: CGRect) -> CGFloat {
  90. // we only need to adjust for the part of the keyboard that covers (i.e. intersects) our collection view;
  91. // see https://developer.apple.com/videos/play/wwdc2017/242/ for more details
  92. let intersection = messagesCollectionView.frame.intersection(keyboardFrame)
  93. if intersection.isNull || (messagesCollectionView.frame.maxY - intersection.maxY) > 0.001 {
  94. // The keyboard is hidden, is a hardware one, or is undocked and does not cover the bottom of the collection view.
  95. // Note: intersection.maxY may be less than messagesCollectionView.frame.maxY when dealing with undocked keyboards.
  96. return max(0, additionalBottomInset - automaticallyAddedBottomInset)
  97. } else {
  98. return max(0, intersection.height + additionalBottomInset - automaticallyAddedBottomInset)
  99. }
  100. }
  101. internal func requiredInitialScrollViewBottomInset() -> CGFloat {
  102. guard let inputAccessoryView = inputAccessoryView else { return 0 }
  103. return max(0, inputAccessoryView.frame.height + additionalBottomInset - automaticallyAddedBottomInset)
  104. }
  105. /// iOS 11's UIScrollView can automatically add safe area insets to its contentInset,
  106. /// which needs to be accounted for when setting the contentInset based on screen coordinates.
  107. ///
  108. /// - Returns: The distance automatically added to contentInset.bottom, if any.
  109. private var automaticallyAddedBottomInset: CGFloat {
  110. if #available(iOS 11.0, *) {
  111. return messagesCollectionView.adjustedContentInset.bottom - messagesCollectionView.contentInset.bottom
  112. } else {
  113. return 0
  114. }
  115. }
  116. }