[:en]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// // 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) } } |
[:]