IQKeyboardManager+UITextFieldViewNotification.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // IQKeyboardManager+UITextFieldViewNotification.swift
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-20 Iftekhar Qurashi.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. // import Foundation - UIKit contains Foundation
  24. import UIKit
  25. // MARK: UITextField/UITextView Notifications
  26. @available(iOSApplicationExtension, unavailable)
  27. internal extension IQKeyboardManager {
  28. private struct AssociatedKeys {
  29. static var textFieldView = "textFieldView"
  30. static var topViewBeginOrigin = "topViewBeginOrigin"
  31. static var rootViewController = "rootViewController"
  32. static var rootViewControllerWhilePopGestureRecognizerActive = "rootViewControllerWhilePopGestureRecognizerActive"
  33. static var topViewBeginOriginWhilePopGestureRecognizerActive = "topViewBeginOriginWhilePopGestureRecognizerActive"
  34. }
  35. /** To save UITextField/UITextView object voa textField/textView notifications. */
  36. weak var textFieldView: UIView? {
  37. get {
  38. return (objc_getAssociatedObject(self, &AssociatedKeys.textFieldView) as? WeakObjectContainer)?.object as? UIView
  39. }
  40. set(newValue) {
  41. objc_setAssociatedObject(self, &AssociatedKeys.textFieldView, WeakObjectContainer(object: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  42. }
  43. }
  44. var topViewBeginOrigin: CGPoint {
  45. get {
  46. return objc_getAssociatedObject(self, &AssociatedKeys.topViewBeginOrigin) as? CGPoint ?? IQKeyboardManager.kIQCGPointInvalid
  47. }
  48. set(newValue) {
  49. objc_setAssociatedObject(self, &AssociatedKeys.topViewBeginOrigin, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  50. }
  51. }
  52. /** To save rootViewController */
  53. weak var rootViewController: UIViewController? {
  54. get {
  55. return (objc_getAssociatedObject(self, &AssociatedKeys.rootViewController) as? WeakObjectContainer)?.object as? UIViewController
  56. }
  57. set(newValue) {
  58. objc_setAssociatedObject(self, &AssociatedKeys.rootViewController, WeakObjectContainer(object: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  59. }
  60. }
  61. /** To overcome with popGestureRecognizer issue Bug ID: #1361 */
  62. weak var rootViewControllerWhilePopGestureRecognizerActive: UIViewController? {
  63. get {
  64. return (objc_getAssociatedObject(self, &AssociatedKeys.rootViewControllerWhilePopGestureRecognizerActive) as? WeakObjectContainer)?.object as? UIViewController
  65. }
  66. set(newValue) {
  67. objc_setAssociatedObject(self, &AssociatedKeys.rootViewControllerWhilePopGestureRecognizerActive, WeakObjectContainer(object: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  68. }
  69. }
  70. var topViewBeginOriginWhilePopGestureRecognizerActive: CGPoint {
  71. get {
  72. return objc_getAssociatedObject(self, &AssociatedKeys.topViewBeginOriginWhilePopGestureRecognizerActive) as? CGPoint ?? IQKeyboardManager.kIQCGPointInvalid
  73. }
  74. set(newValue) {
  75. objc_setAssociatedObject(self, &AssociatedKeys.topViewBeginOriginWhilePopGestureRecognizerActive, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  76. }
  77. }
  78. /** UITextFieldTextDidBeginEditingNotification, UITextViewTextDidBeginEditingNotification. Fetching UITextFieldView object. */
  79. @objc func textFieldViewDidBeginEditing(_ notification: Notification) {
  80. guard let object = notification.object as? UIView, let isKeyWindow = object.window?.isKeyWindow, isKeyWindow else {
  81. return
  82. }
  83. let startTime = CACurrentMediaTime()
  84. showLog("📝>>>>> \(#function) started >>>>>", indentation: 1)
  85. showLog("Notification Object:\(notification.object ?? "NULL")")
  86. // Getting object
  87. textFieldView = notification.object as? UIView
  88. if overrideKeyboardAppearance, let textInput = textFieldView as? UITextInput, textInput.keyboardAppearance != keyboardAppearance {
  89. //Setting textField keyboard appearance and reloading inputViews.
  90. if let textFieldView = textFieldView as? UITextField {
  91. textFieldView.keyboardAppearance = keyboardAppearance
  92. } else if let textFieldView = textFieldView as? UITextView {
  93. textFieldView.keyboardAppearance = keyboardAppearance
  94. }
  95. textFieldView?.reloadInputViews()
  96. }
  97. //If autoToolbar enable, then add toolbar on all the UITextField/UITextView's if required.
  98. if privateIsEnableAutoToolbar() {
  99. //UITextView special case. Keyboard Notification is firing before textView notification so we need to resign it first and then again set it as first responder to add toolbar on it.
  100. if let textView = textFieldView as? UIScrollView, textView.responds(to: #selector(getter: UITextView.isEditable)),
  101. textView.inputAccessoryView == nil {
  102. UIView.animate(withDuration: 0.00001, delay: 0, options: animationCurve, animations: { () -> Void in
  103. self.addToolbarIfRequired()
  104. }, completion: { (_) -> Void in
  105. //On textView toolbar didn't appear on first time, so forcing textView to reload it's inputViews.
  106. textView.reloadInputViews()
  107. })
  108. } else {
  109. //Adding toolbar
  110. addToolbarIfRequired()
  111. }
  112. } else {
  113. removeToolbarIfRequired()
  114. }
  115. resignFirstResponderGesture.isEnabled = privateShouldResignOnTouchOutside()
  116. textFieldView?.window?.addGestureRecognizer(resignFirstResponderGesture) // (Enhancement ID: #14)
  117. if privateIsEnabled() == false {
  118. restorePosition()
  119. topViewBeginOrigin = IQKeyboardManager.kIQCGPointInvalid
  120. } else {
  121. if topViewBeginOrigin.equalTo(IQKeyboardManager.kIQCGPointInvalid) { // (Bug ID: #5)
  122. rootViewController = textFieldView?.parentContainerViewController()
  123. if let controller = rootViewController {
  124. if rootViewControllerWhilePopGestureRecognizerActive == controller {
  125. topViewBeginOrigin = topViewBeginOriginWhilePopGestureRecognizerActive
  126. } else {
  127. topViewBeginOrigin = controller.view.frame.origin
  128. }
  129. rootViewControllerWhilePopGestureRecognizerActive = nil
  130. topViewBeginOriginWhilePopGestureRecognizerActive = IQKeyboardManager.kIQCGPointInvalid
  131. self.showLog("Saving \(controller) beginning origin: \(self.topViewBeginOrigin)")
  132. }
  133. }
  134. //If textFieldView is inside ignored responder then do nothing. (Bug ID: #37, #74, #76)
  135. //See notes:- https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html If it is UIAlertView textField then do not affect anything (Bug ID: #70).
  136. if keyboardShowing,
  137. let textFieldView = textFieldView,
  138. textFieldView.isAlertViewTextField() == false {
  139. // keyboard is already showing. adjust position.
  140. optimizedAdjustPosition()
  141. }
  142. }
  143. let elapsedTime = CACurrentMediaTime() - startTime
  144. showLog("📝<<<<< \(#function) ended: \(elapsedTime) seconds <<<<<", indentation: -1)
  145. }
  146. /** UITextFieldTextDidEndEditingNotification, UITextViewTextDidEndEditingNotification. Removing fetched object. */
  147. @objc func textFieldViewDidEndEditing(_ notification: Notification) {
  148. guard let object = notification.object as? UIView, let isKeyWindow = object.window?.isKeyWindow, isKeyWindow else {
  149. return
  150. }
  151. let startTime = CACurrentMediaTime()
  152. showLog("📝>>>>> \(#function) started >>>>>", indentation: 1)
  153. showLog("Notification Object:\(notification.object ?? "NULL")")
  154. //Removing gesture recognizer (Enhancement ID: #14)
  155. textFieldView?.window?.removeGestureRecognizer(resignFirstResponderGesture)
  156. // We check if there's a change in original frame or not.
  157. if let textView = textFieldView as? UIScrollView, textView.responds(to: #selector(getter: UITextView.isEditable)) {
  158. if isTextViewContentInsetChanged {
  159. self.isTextViewContentInsetChanged = false
  160. if textView.contentInset != self.startingTextViewContentInsets {
  161. self.showLog("Restoring textView.contentInset to: \(self.startingTextViewContentInsets)")
  162. UIView.animate(withDuration: animationDuration, delay: 0, options: animationCurve, animations: { () -> Void in
  163. //Setting textField to it's initial contentInset
  164. textView.contentInset = self.startingTextViewContentInsets
  165. textView.scrollIndicatorInsets = self.startingTextViewScrollIndicatorInsets
  166. }, completion: { (_) -> Void in })
  167. }
  168. }
  169. }
  170. //Setting object to nil
  171. #if swift(>=5.7)
  172. if #available(iOS 16.0, *), let textView = object as? UITextView, textView.isFindInteractionEnabled {
  173. //Not setting it nil, because it may be doing find interaction.
  174. //As of now, here textView.findInteraction?.isFindNavigatorVisible returns false
  175. //So there is no way to detect if this is dismissed due to findInteraction
  176. } else {
  177. textFieldView = nil
  178. }
  179. #else
  180. textFieldView = nil
  181. #endif
  182. let elapsedTime = CACurrentMediaTime() - startTime
  183. showLog("📝<<<<< \(#function) ended: \(elapsedTime) seconds <<<<<", indentation: -1)
  184. }
  185. }