2 // IQKeyboardReturnKeyHandler.swift
3 // https://github.com/hackiftekhar/IQKeyboardManager
4 // Copyright (c) 2013-24 Iftekhar Qurashi.
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
27 Manages the return key to work like next/done in a view hierarchy.
29 @available(iOSApplicationExtension, unavailable)
31 @objc public final class IQKeyboardReturnKeyHandler: NSObject {
36 Delegate of textField/textView.
38 @objc public weak var delegate: (UITextFieldDelegate & UITextViewDelegate)?
41 Set the last textfield return key type. Default is UIReturnKeyDefault.
43 @objc public var lastTextFieldReturnKeyType: UIReturnKeyType = UIReturnKeyType.default {
46 for model in textFieldInfoCache {
47 if let view: UIView = model.textFieldView {
48 updateReturnKeyTypeOnTextField(view)
54 // MARK: Initialization/De-initialization
56 @objc public override init() {
61 Add all the textFields available in UIViewController's view.
63 @objc public init(controller: UIViewController) {
66 addResponderFromView(controller.view)
71 // for model in textFieldInfoCache {
75 textFieldInfoCache.removeAll()
78 // MARK: Private variables
79 private var textFieldInfoCache: [IQTextFieldViewInfoModel] = []
81 // MARK: Private Functions
82 internal func textFieldViewCachedInfo(_ textField: UIView) -> IQTextFieldViewInfoModel? {
84 for model in textFieldInfoCache {
86 if let view: UIView = model.textFieldView {
87 if view == textField {
96 internal func updateReturnKeyTypeOnTextField(_ view: UIView) {
97 var superConsideredView: UIView?
99 // If find any consider responderView in it's upper hierarchy then will get deepResponderView. (Bug ID: #347)
100 for allowedClasse in IQKeyboardManager.shared.toolbarPreviousNextAllowedClasses {
102 superConsideredView = view.iq.superviewOf(type: allowedClasse)
104 if superConsideredView != nil {
109 var textFields: [UIView] = []
111 // If there is a tableView in view's hierarchy, then fetching all it's subview that responds.
112 if let unwrappedTableView: UIView = superConsideredView { // (Enhancement ID: #22)
113 textFields = unwrappedTableView.iq.deepResponderViews()
114 } else { // Otherwise fetching all the siblings
116 textFields = view.iq.responderSiblings()
118 // Sorting textFields according to behavior
119 switch IQKeyboardManager.shared.toolbarConfiguration.manageBehavior {
120 // If needs to sort it by tag
121 case .byTag: textFields = textFields.sortedByTag()
122 // If needs to sort it by Position
123 case .byPosition: textFields = textFields.sortedByPosition()
128 if let lastView: UIView = textFields.last {
130 if let textField: UITextField = view as? UITextField {
132 // If it's the last textField in responder view, else next
133 textField.returnKeyType = (view == lastView) ? lastTextFieldReturnKeyType: UIReturnKeyType.next
134 } else if let textView: UITextView = view as? UITextView {
136 // If it's the last textField in responder view, else next
137 textView.returnKeyType = (view == lastView) ? lastTextFieldReturnKeyType: UIReturnKeyType.next
142 // MARK: Registering/Unregistering textFieldView
145 Should pass UITextField/UITextView instance. Assign textFieldView delegate to self, change it's returnKeyType.
147 @param view UITextField/UITextView object to register.
149 @objc public func addTextFieldView(_ view: UIView) {
151 if let textField: UITextField = view as? UITextField {
152 let model = IQTextFieldViewInfoModel(textField: textField)
153 textFieldInfoCache.append(model)
154 textField.delegate = self
156 } else if let textView: UITextView = view as? UITextView {
157 let model = IQTextFieldViewInfoModel(textView: textView)
158 textFieldInfoCache.append(model)
159 textView.delegate = self
164 Should pass UITextField/UITextView instance. Restore it's textFieldView delegate and it's returnKeyType.
166 @param view UITextField/UITextView object to unregister.
168 @objc public func removeTextFieldView(_ view: UIView) {
170 if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(view) {
173 if let index: Int = textFieldInfoCache.firstIndex(where: { $0.textFieldView == view}) {
174 textFieldInfoCache.remove(at: index)
180 Add all the UITextField/UITextView responderView's.
182 @param view UIView object to register all it's responder subviews.
184 @objc public func addResponderFromView(_ view: UIView) {
186 let textFields: [UIView] = view.iq.deepResponderViews()
188 for textField in textFields {
190 addTextFieldView(textField)
195 Remove all the UITextField/UITextView responderView's.
197 @param view UIView object to unregister all it's responder subviews.
199 @objc public func removeResponderFromView(_ view: UIView) {
201 let textFields: [UIView] = view.iq.deepResponderViews()
203 for textField in textFields {
205 removeTextFieldView(textField)
210 internal func goToNextResponderOrResign(_ view: UIView) -> Bool {
212 var superConsideredView: UIView?
214 // If find any consider responderView in it's upper hierarchy then will get deepResponderView. (Bug ID: #347)
215 for allowedClass in IQKeyboardManager.shared.toolbarPreviousNextAllowedClasses {
217 superConsideredView = view.iq.superviewOf(type: allowedClass)
219 if superConsideredView != nil {
224 var textFields: [UIView] = []
226 // If there is a tableView in view's hierarchy, then fetching all it's subview that responds.
227 if let unwrappedTableView: UIView = superConsideredView { // (Enhancement ID: #22)
228 textFields = unwrappedTableView.iq.deepResponderViews()
229 } else { // Otherwise fetching all the siblings
231 textFields = view.iq.responderSiblings()
233 // Sorting textFields according to behavior
234 switch IQKeyboardManager.shared.toolbarConfiguration.manageBehavior {
235 // If needs to sort it by tag
236 case .byTag: textFields = textFields.sortedByTag()
237 // If needs to sort it by Position
238 case .byPosition: textFields = textFields.sortedByPosition()
244 // Getting index of current textField.
245 if let index: Int = textFields.firstIndex(of: view) {
246 // If it is not last textField. then it's next object becomeFirstResponder.
247 if index < (textFields.count - 1) {
249 let nextTextField: UIView = textFields[index+1]
250 nextTextField.becomeFirstResponder()
254 view.resignFirstResponder()