Search This Blog

Monday, February 27, 2017

3 ways to hide keyboard in ios

There is one of most popular issues in ios development. How to hide keyboard on touch outside or when Return button is tapped in keyboard. There are different ways how to do it.


1. override func touchesBegan


The easiest one. Just override one method of UIView - touchesBegan and invoke method endEditing of view.

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
   
}

2. Using UITapGestureRecognizer


Create gesture recognizer for tap gestures and add it to the view. On gesture handler hide the keyboard.

class ViewController: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let tapRecognizer = UITapGestureRecognizer(target: self, 
            action: #selector(ViewController.dismissKeyboard))
        self.view.addGestureRecognizer(tapRecognizer)
    }
    
    func dismissKeyboard() {
        self.textField.resignFirstResponder()
    }
    
}

3. Using UITextFieldDelegate

There is how to hide keyboard when Return button of Keyboard is tapped. We need to be UITextFieldDelegate, make self as delegate and implement textFieldShouldReturn method in which we will hide keyboard.

class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.textField.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.dismissKeyboard()
        return false
    }
   
}



7 comments:

  1. Much obliged for such a useful information. I have read it completely and cant find a match to your information anywhere else. It was so unique and intellectual and the way you elaborated. It was worth praising. Hope to see more and more informative stuff coming from you.
    -Web Development Services

    ReplyDelete
  2. Your article is one of its kind which explained every bit of Custom Build Website. looking for further valuable articles from you

    ReplyDelete
  3. You've written a fascinating piece of writing. This is exactly the kind of information I was looking for. Please provide me with more relevant information so that I can learn more. Best Custom Websites

    ReplyDelete
  4. Thank you very much for drawing my notice to this. Your blog is jam-packed with useful facts. Until I read that, I would have no idea. I'll come back for more fantastic material.
    Best wishes, and good luck.
    Web Design USA

    ReplyDelete
  5. Good content. I was looking for this type of content. Saved my time for further search.
    You should always provide that type of content. I appreciate your determination to give the students a good title.Custom Designed Websites

    ReplyDelete
  6. Great content. I was looking for this kind of content. It saved my time to search further.
    You must provide such type of content constantly. I appreciate your willingness to provide readers with a good article.
    Mobile Performance Meter Hack

    ReplyDelete
  7. Great share!One of the best blogs I have Ever Seen. Keep going!
    QA Testing Services

    ReplyDelete