[:en]
// // ViewController.swift // HideKeyboard // // Created by Cambridge on 10/4/2019. // Copyright © 2019 Cambridge . All rights reserved. // import UIKit class ViewController: UIViewController , UITextFieldDelegate{ @IBOutlet weak var studentnamelabel: UITextField! override func viewDidLoad() { super.viewDidLoad() //to hide keyboard when tapping on empty space self.setupToHideKeyboardOnTapOnView() } } //put the extension at the bottem of the code also known as "outside" the viewcontroller extension UIViewController { func setupToHideKeyboardOnTapOnView() { let tap: UITapGestureRecognizer = UITapGestureRecognizer( target: self, action: #selector(UIViewController.dismissKeyboard)) tap.cancelsTouchesInView = false view.addGestureRecognizer(tap) } @objc func dismissKeyboard() { view.endEditing(true) } }
[:]