Close iOS Keyboard by touching anywhere using Swift

swift hide keyboard programmatically
hide keyboard search bar swift
dismiss a view when a user taps anywhere outside the view swift 4
swift dismiss keyboard on return
keyboard hide and show in swift
dismiss keyboard uitextview swift
keyboard ios swift
iqkeyboardmanager hide keyboard

I have been looking all over for this but I can't seem to find it. I know how to dismiss the keyboard using Objective-C but I have no idea how to do that using Swift? Does anyone know?


override func viewDidLoad() {
    super.viewDidLoad()

    //Looks for single or multiple taps. 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")

    //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
    //tap.cancelsTouchesInView = false 

    view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

Here is another way to do this task if you are going to use this functionality in multiple UIViewControllers:

// Put this piece of code anywhere you like
extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false            
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

Now in every UIViewController, all you have to do is call this function:

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround() 
}

This function is included as a standard function in my repo which contains a lot of useful Swift Extensions like this one, check it out: https://github.com/goktugyil/EZSwiftExtensions

Dismiss/Hide Keyboard by touching Anywhere outside UITextField , Dismiss/Hide Keyboard by touching Anywhere outside UITextField iOS (swift) 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. How to dismiss a keyboard on tap outside in iOS (swift) : In this tutorial, we will learn how to dismiss a keyboard if the user taps on outside on any view. In iOS, the keyboard doesn’t dismiss automatically if the user taps anywhere outside of the keyboard view. So, we need to handle it manually.


An answer to your question on how to dismiss the keyboard in Xcode 6.1 using Swift below:

import UIKit

class ItemViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textFieldItemName: UITextField!

    @IBOutlet var textFieldQt: UITextField!

    @IBOutlet var textFieldMoreInfo: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()

        textFieldItemName.delegate = self
        textFieldQt.delegate = self
        textFieldMoreInfo.delegate = self
    }

                       ...

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


   /**
    * Called when the user click on the view (outside the UITextField).
    */
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }

}

(Source of this information).

iOS Notes 31: How to hide the keyboard by touching anywhere , Updated: 14 Dec, 2019. “iOS Notes 31: How to hide the keyboard by touching anywhere? [Updated v2]” is published by Kuray Ogun in  I was thinking to write about all possible ways to dismiss the keyboard in iOS app and let people figure out which one is best suited one. UITextFields has keyboard as first responders, as soon you…


Swift 4 working

Create extension as below & call hideKeyboardWhenTappedAround() in your Base view controller.

//
//  UIViewController+Extension.swift
//  Project Name
//
//  Created by ABC on 2/3/18.
//  Copyright © 2018 ABC. All rights reserved.
//

import UIKit

extension UIViewController {
    func hideKeyboardWhenTappedAround() {
        let tapGesture = UITapGestureRecognizer(target: self, 
                         action: #selector(hideKeyboard))
        view.addGestureRecognizer(tapGesture)
    }

    @objc func hideKeyboard() {
        view.endEditing(true)
    }
}

Most important thing to call in your Base View Controller so that no need to call all time in all view controllers.

How To Hide The Keyboard in iOS - Xcode 9.1 (Swift 4), Close iOS Keyboard by touching anywhere using Swift. 3. How to Dismiss UITextField's Duration: 4:21 Posted: Dec 22, 2017 Close iOS Keyboard by touching anywhere using Swift. 206. Swift: Test class type in switch statement. 318. How close to a building has a Soyuz landed?


You can call

resignFirstResponder()

on any instance of a UIResponder, such as a UITextField. If you call it on the view that is currently causing the keyboard to be displayed then the keyboard will dismiss.

HIDE KEYBOARD (Swift-4, Xcode-9) ( WHEN TAP ANYWHERE , This video is about How to hide keyboard when you tap anywhere on the screen. SWIFT WITH Duration: 4:55 Posted: Apr 21, 2018 Now, how can i dismiss this view by touching anywhere outside the view. From the research, i found that, it can be done by creating a transparent button that fits the whole viewController. So then by clicking on the button, we can make the view to dismiss. Can anyone give me the code in swift 3 to create such button.


//Simple exercise to demonstrate, assuming the view controller has a //Textfield, Button and a Label. And that the label should display the //userinputs when button clicked. And if you want the keyboard to disappear //when clicken anywhere on the screen + upon clicking Return key in the //keyboard. Dont forget to add "UITextFieldDelegate" and 
//"self.userInput.delegate = self" as below

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var userInput: UITextField!
    @IBAction func transferBtn(sender: AnyObject) {
                display.text = userInput.text

    }
    @IBOutlet weak var display: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

//This is important for the textFieldShouldReturn function, conforming to textfieldDelegate and setting it to self
        self.userInput.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

//This is for the keyboard to GO AWAYY !! when user clicks anywhere on the view
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }


//This is for the keyboard to GO AWAYY !! when user clicks "Return" key  on the keyboard

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

}

How to dismiss iOS keyboard programatically (Swift 5), "Handling Keyboard in iOS" - this has been a topic or rather a done button which will dismiss the keyboard, tapping outside the keyboard the text-field on the screen that was placed near the bottom of the screen. Now when you tap on a text-field and keyboard appears, just tap outside anywhere on  You can make the app dismiss the keyboard using the following function. func textFieldShouldReturn(_ textField: UITextField) -> Bool { self.view.endEditing(true) return false } Here is a full example to better illustrate that:


dismiss keyboard when tap outside swift Code Example, Get code examples like "dismiss keyboard when tap outside swift" instantly right from Close iOS Keyboard by touching anywhere using Swift  touch the "note" field (which becomes first responder and fires up the keyboard); swipe left on any other field to make the "delete" button appear; by this point you have no first responder among the text fields (just check programmatically) but the keyboard is still there. Calling [view endEditing:YES] does nothing.


iOS, If you want to hide a keyboard by tap outside of it, it's possible to use this hacky trick (works only with Objective-C): Another Swift 3/iOS 10 example //Hide the keyboard for any text field when the UI is touched outside of the keyboard. If you need to take text input in your Swift app, you will probably need a UITextField. It is exactly what it sounds like, just a field on the screen where the user types something in. When the user taps on it, the keyboard comes up from the bottom of the screen, and allows the user to start typing.


https://stackoverflow.com/questions/24126678/close-ios-keyboard , I have been looking all over for this but I can't seem to find it. I know how to dismiss the keyboard using Objective-C but I have no idea how to do that using Swift? Resize the screen when keyboard appears. Thanks for contributing an answer to Stack Overflow! Close iOS Keyboard by touching anywhere using Swift. 667.