0
votes

I'm using this pod in order to work on text fields and keyboard. In my App.Delegate file, I imported IQKeyboardManagerSwift and enabled it, which works fine now. However, the problem I have now is even when the user tap return key on the keyboard, I cannot dismiss the keyboard and the keyboard remains the same. I added IQKeyboardManager.shared.shouldResignOnTouchOutside = true, so when the user taps the outside of the keyboard, the keyboard dismisses. I want to add the functionality that when the user tap return button on the keyboard, the keyboard also dismisses. I guess resignFirstResponder() will enable this function (Resigns currently first responder field), but I don't know how to implement that function.

I saw some other people use textfieldShouldReturn function, but I think I have to make the function several times if I add code in the view controllers. Currently, I have several text fields, so if possible, I just want to write code only one time.

How can I implement that function?

import IQKeyboardManagerSwift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {


var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    IQKeyboardManager.shared.enable = true
    IQKeyboardManager.shared.enableAutoToolbar = false
    IQKeyboardManager.shared.shouldResignOnTouchOutside = true
    IQKeyboardManager.shared.resignFirstResponder()
    
    
    return true
}
....
}
1
just use textfieldShouldReturn, you only need to write it one timegoat_herd
@goat_herd I thought if I use textfieldShoudReturn, I need to specify which text field I'm going to end editing, like emailTextField.endEditing(true). If I use textfieldShouldReturn, what is gonna be look like in the body of the function and where I should put the function?? Sorry, I'm super beginer and need time for understand everything...zzzzou
no, you should use this func: view.endEditing(true). so you don't need to check what your textField isgoat_herd
I see. So where I should put the textfieldshouldReturn function? Is it in the AppDelegate?zzzzou
no, set delegate of your textField to self in viewDidLoad, and then make your ViewController conform to UITextFiledDelegate and implement textFieldSouldReturn in there, I has post the answer for illustrationgoat_herd

1 Answers

1
votes

Set delegate for all your textField to your viewController:

yourTextField.delegate = self

then make your viewController comfort to UITextField Delegate:

// MARK: - UITextFieldDelegate
extension YourViewController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        view.endEditing(true)
    }
}