Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the keyboard using ‘done’ button Swift 4

I was wondering how can I hide the keyboard using the ‘done’ button when I’m entering in a UITextField.

Thank you,

Unknown.

like image 788
Unknown_ Avatar asked Mar 27 '18 21:03

Unknown_


2 Answers

use below code

class ViewController: UIViewController,UITextFieldDelegate

  // set delegate in didLoad
     override func viewDidLoad() {
       super.viewDidLoad()
       textField.delegate = self
     }


   // MARK: - Search Method
    func textFieldShouldReturn(_ textField: UITextField) -> Bool
    {

        textField.resignFirstResponder()
        return true
    }
like image 72
iOS Geek Avatar answered Sep 22 '22 04:09

iOS Geek


Set the delegate on the text field and implement this method. Should do the trick.

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}
like image 32
Patrick O'Malley Avatar answered Sep 19 '22 04:09

Patrick O'Malley