I want to dismiss keypad when tap on anywhere on screen. For that I used touchbegan
method but this method was not called.
Asked
Active
Viewed 509 times
1

lukkea
- 3,606
- 2
- 37
- 51

amol ambhure
- 45
- 6
-
1show what you are trying..? – Anil Kumar May 16 '17 at 07:25
-
`touchBegan` on what? What view? – Max Pevsner May 16 '17 at 07:25
-
Educated guess , since i have no information on your code or views hirerchy . some other view prorbaly "steal" your touches . – Nevgauker May 16 '17 at 07:31
-
show your code! – Ketan Parmar May 16 '17 at 07:44
-
Possible duplicate of [Close iOS Keyboard by touching anywhere using Swift](http://stackoverflow.com/questions/24126678/close-ios-keyboard-by-touching-anywhere-using-swift) – Mr. Bean May 16 '17 at 07:45
-
If my answer helped, accept it pls. – Vlad Pulichev Jun 08 '17 at 07:24
5 Answers
1
You need to assign UITapGestureRecogniser
to view and dismiss keyboard.
In viewdidLoad()
let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.dismissKeybord(_:)))
tap.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tap)
In dismissKeybord
func dismissKeybord(_ sender:UITapGestureRecognizer) {
self.view.endEditing(true)
}

Hitesh
- 896
- 1
- 9
- 22
0
Working for me. Showing keyboard on touch to textfields
. Hide on every other touches.
class LoginController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var userLogin: UITextField!
@IBOutlet weak var userPassword: UITextField!
override func viewDidLoad() {
userLogin.delegate = self
userPassword.delegate = self
//For scrolling the view if keyboard on
NotificationCenter.default.addObserver(self, selector: #selector(LoginController.keyboardWillShow),
name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(LoginController.keyboardWillHide),
name: NSNotification.Name.UIKeyboardWillHide, object: nil)
super.viewDidLoad()
}
// OTHER METHODS
var keyBoardAlreadyShowed = false //using this to not let app to scroll view
//if we tapped UITextField and then another UITextField
func keyboardWillShow(notification: NSNotification) {
if !keyBoardAlreadyShowed {
view.frame.origin.y -= 50
keyBoardAlreadyShowed = true
}
}
func keyboardWillHide(notification: NSNotification) {
view.frame.origin.y += 50
keyBoardAlreadyShowed = false
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)
}
}
You need to set delegates
and observers

Vlad Pulichev
- 3,162
- 2
- 20
- 34
0
If your touches are not eaten by other view, then i guess this will work
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if touches.first != nil {
view.endEditing(true)
}
}

Anil Kumar
- 945
- 7
- 16
0
Swift 3 :-
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

Pratyush Pratik
- 683
- 7
- 14
0
This worked in my case recently. Should work for you too.
- Create a UITapGestureRecognizer.
- In the selector method that is endEditing in the below code, simply call self.view.endEditing(true).
(Please note, the parameter true is a value for animated)
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(endEditing))
tapper.cancelsTouchesInView = false
self.view.addGestureRecognizer(tapGestureRecognizer)
}
func endEditing() {
self.view.endEditing(true)
}
}

Saurabh Bhatia
- 1,875
- 1
- 20
- 29