7

How would you dismiss the keyboard from the UITextField when tapping outside of the keyboard. I have tried resignFirstResponder() and it only exits after typing one number. I have also tried textField.inputView = UIView.frame(frame: CGRectZero). I have seen many Obj-C verisons of what I'm asking but I need the Swift equivalent because I have no programming experience in Objective-C

Thank you for your time and patience.

rebello95
  • 8,486
  • 5
  • 44
  • 65

5 Answers5

18

The best way to add a tap gesture recognizer to the view and calling either resignFirstResponder() or self.view.endEditing(true). I prefer endEditing() since resignFirstResponder has to be done for each text field separately unlike endEditing which is done for the view itself.

In viewDidLoad, write the below code:

let tapRecognizer = UITapGestureRecognizer()
tapRecognizer.addTarget(self, action: "didTapView")
self.view.addGestureRecognizer(tapRecognizer)

Now write the didTapView method to dismiss the keyboard:

func didTapView(){
  self.view.endEditing(true)
}

Now when you tap outside the keyboard on the main view of the controller, it will call the didTapView method and dismiss the keyboard.

Swift 3.x

The code in viewDidLoad should be:

let tapRecognizer = UITapGestureRecognizer()
tapRecognizer.addTarget(self, action: #selector(ViewController.didTapView))
self.view.addGestureRecognizer(tapRecognizer)

where ViewController should be the name of your view controller.

Thanks

momentsnapper
  • 605
  • 1
  • 5
  • 11
5

Swift 3 tested and working

 // dismiss keyboard on touch outside textfield
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for txt in self.view.subviews {
            if txt.isKind(of: UITextField.self) && txt.isFirstResponder {
                txt.resignFirstResponder()
            }
        }
    }   

Enjoy

Swift 2.3 tested and working

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for txt in self.view.subviews {
            if txt.isKindOfClass(UITextField.self) && txt.isFirstResponder() {
                txt.resignFirstResponder()
            }
        }
    }

Enjoy

Stan
  • 1,513
  • 20
  • 27
2

You could also use this method to dismiss the keyboard when pressing 'Return'

func textFieldShouldReturn(textField: UITextField!) -> Bool {
        self.view.endEditing(true);
        return false;
}

Make sure to set your delegate

alex
  • 1,746
  • 6
  • 19
  • 34
0

If you don't want to define an extra method, there is a slightly simpler way that will also work

let tapRecognizer = UITapGestureRecognizer(target: self, action: "endEditing:")
view.addGestureRecognizer(tapRecognizer)
Jawwad
  • 1,326
  • 2
  • 9
  • 18
0

I found this code on a site and it works great for me!

//FUNCTION TO DISMISS THE KEYBOARD
 func initializeHideKeyboard(){
 //Declare a Tap Gesture Recognizer which will trigger our dismissMyKeyboard() function
 let tap: UITapGestureRecognizer = UITapGestureRecognizer(
 target: self,
 action: #selector(dismissMyKeyboard))
 //Add this tap gesture recognizer to the parent view
 view.addGestureRecognizer(tap)
 }

 @objc func dismissMyKeyboard(){
 //endEditing causes the view (or one of its embedded text fields) to resign the first responder status.
 //In short- Dismiss the active keyboard.
 view.endEditing(true)
 }

Then just call this function in a button action or similar:

dismissMyKeyboard()
David_2877
  • 257
  • 4
  • 7