Close iOS Keyboard by touching anywhere using Swift 3

How to close your keyboard by touching a view or return button on the keyboard for UITextField


import UIKit
class SendDropMailVC: UIViewController , UITextFieldDelegate {
   
    @IBOutlet weak var TextField: UITextField!
      
    override func viewDidLoad() {
        super.viewDidLoad()
        //to recognize delegate
        self.TextField.delegate = self
    }
    
    // When you touch anywhere on your view.
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    //when you touch a Return Button on the keyboard.
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        TextField.resignFirstResponder()
        return (true)
    }
    

}

Comments