hide keyboard for text field in swift programming language

Solution 1:

I think the Problem is with setting the Delegate.

Please set textfield Delegate to your ViewController like this

class ViewController: UIViewController,UITextFieldDelegate {

and then create the IBOutlet for your text field like this

@IBOutlet var txtTest : UITextField = nil

make sure that it is connected to your text field on your view in storyboard.

finally set its Delegate using

txtTest.delegate=self

We are almost done. Now place this delegate function into your class.

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
    {
        textField.resignFirstResponder()
        return true;
    }

Hope this will solve your issue.

Solution 2:

Just override the UIViewController method called "touchesBegan" and set endEditing to true. Just like this:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
    self.view.endEditing(true)
}

Solution 3:

In order to hide the keyboard when you pressed a button, you need to use the following function.

self.view.endEditing(true)

This works when a button is pressed.

Hope this helps someone out there.

Solution 4:

Here we go:

  1. Add a textField to your View
  2. Create a new Swift file
  3. Set this new file as a Class for that particular View
  4. Add a TextField to your View
  5. Create an Outlet for the textfield (my is named "txtField" !)
  6. Substitute any code in the Swift Class file with this:

    import Foundation
    import UIKit
    
    //01 create delegation
    class MyViewController2: UIViewController,UITextFieldDelegate {
    
      @IBOutlet weak var txtField: UITextField!=nil
    
        override func viewDidLoad() {
          super.viewDidLoad()
          // additional setup after loading the view
    
          //02 set delegate to textfield
          txtField.delegate = self
        }
    
        //03 textfield func for the return key
        func textFieldShouldReturn(textField: UITextField) -> Bool {
          txtField.resignFirstResponder()
          return true;
        }
    
        //textfield func for the touch on BG
        override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
          txtField.resignFirstResponder()
          self.view.endEditing(true)
        }
    
        override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          //dispose of any resources that can be recreated
        }
      }
    
    1. Try it out, be happy & say thanks !

Solution 5:

Now In Swift 3/4/5, the easiest methods are

Method 1: called when 'return' key pressed.

func textFieldShouldReturn(_ textField: UITextField) -> Bool 
 {
 textField1.resignFirstResponder()
 textField2.resignFirstResponder()
        return true;
    }

Method 2: called when 'return' key pressed.

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

Method 3: Global Method Write in view did load

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))

Note: Don't forget to add this. Other wise its not work.

UIGestureRecognizerDelegate, UITextFieldDelegate

I hope it will work for you :)