Apoorv Blog

Hide/Close Keyboard After Typing in UITextfield

Updated on:



Screenshot Tutorial

1. First let’s setup the XCode project as usual. Create new single view application X code project. Set project name to hide keyboard and save it to the desktop.

Set project name

2. Go to the main storyboard and add 3 textfields on the view controller.

add textfields

3. Then in view controller swift file you just override touches began and on touch event any view that is in editing mode you end it forcefully.

11
12
13
14
15
16
17
18
19
20
21
22
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        view.endEditing(true)
    }
}

3. Now if you just run the app on iPhone XR then you can close / hide keyboard if you just tapped outside the textfield. This works for single textfield as well as multiple textfields.

demo hiding keyboard

4. If you want to close keyboard on pressing the return button then connnect delegate for each textfield to the view controller. Do this for every textfield.

textfield delegate to vc

5. Add textfield delegate as subclass to view controller.

9
10
11
12
13
import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {

6. Add single delegate method. When you press return button then you call resign first responder to stop editing textfield.

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class ViewController: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        view.endEditing(true)
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        
        return true
    }
}

7. Now if you just run the app on iPhone XR then you can close / hide keyboard when you press return key on the keyboard. This works for single textfield as well as multiple textfields.

demo hiding keyboard

8. If you need to close keyboard when pressed on submit or buy now button then add button in the view controller and set its name Buy Now / Submit.

add button

9. Open assitant editor and create outlet for first textfield. And name it textField1.

textfield outlet

10. Also create action for submit button. And name it Submit Pressed.

Button action

11. Then simply add single line in the submit pressed action to stop editing of the textfield.

24
25
26
27
28
29
30
31
32
33
34
35
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        
        return true
    }
    
    @IBAction func submitPressed(_ sender: UIButton) {
        
        textField1.resignFirstResponder()
    }
}

12. Now if you just run the app on iPhone XR then you can close / hide keyboard when you press submit button.

demo hiding keyboard

comments powered by Disqus