Dismiss/Hide Keyboard by touching Anywhere outside UITextField iOS (swift)

sruthi sankar
2 min readNov 23, 2018

--

When the mobile users touch a text field or any similar related to a text field, the system displays a keyboard. We can configure the type of keyboard that should be displayed along with several other attributes of the same. We can also manage the keyboard when the editing session starts and ends. Because the keyboard could hide the portion of your view , we can use a function to dismiss keyboard if it touches outside the typing fields.

Create an extension of the view controller

extension UIViewController {

func dismissKey()

{

let tap: UITapGestureRecognizer = UITapGestureRecognizer( target: self, action: #selector(UIViewController.dismissKeyboard))

tap.cancelsTouchesInView = false view.addGestureRecognizer(tap)

}

@objc func dismissKeyboard()

{

view.endEditing(true)

}

}

A simple function is used to hide the keyboard, and we can use the function any where by using “self.hideKeyboard()” in the viewDidLoad.

It is the best way to solve the issue of “hide keyboard”….

In my next article we can see again and is related to searchbar….

--

--