Close iOS Keyboard by touching anywhere using Swift
override func viewDidLoad() {
super.viewDidLoad()
//Looks for single or multiple taps.
let tap = UITapGestureRecognizer(target: self, action: #selector(UIInputViewController.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(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 keyboard in iOS
first of all write this extension in any swift file
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
}
Than in viewDidLoad of that View only call in any view controller there are textFields.
self.hideKeyboardWhenTappedAround()
How to dismiss keyboard when touching anywhere outside UITextField (in swift)?
Edited for Swift 4
Edit: Added @objc
. While this isn't the best option for performance, one instance of it here shouldn't cause too many problems until there is a better solution.
Edited to fix when needing to interact with items behind GestureRecognizer.
Edit: Thanks @Rao for pointing this out. Added tap.cancelsTouchesInView = false
.
This should help you with having multiple UITextView
or UITextField
Create an extension of the view controller. This has worked much smoother for me and with less hassle than trying to use .resignFirstResponder()
extension UIViewController
{
func setupToHideKeyboardOnTapOnView()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard()
{
view.endEditing(true)
}
}
Call self.setupToHideKeyboardOnTapOnView()
in the viewDidLoad
how to Dismiss Keyboard if we touch other area outside UITextView in Swift
option 1
Without blocking the UI add the cancelsTouchesInView
for your gesture `
let tapGestureReconizer = UITapGestureRecognizer(target: self, action: "tap:")
tapGestureReconizer.cancelsTouchesInView = false
view.addGestureRecognizer(tapGestureReconizer)
option 2
else handle the resign in multiple ways
func tap(sender: UITapGestureRecognizer) {
view.endEditing(true)
// or use
noteTextView.resignFirstResponder()
// or use
view.super().endEditing(true)
// or use
view.keyboardDismissMode = .onDrag
}
How to hide Keyboard which is in another window? Swift
First, get a reference to host app keyWindow
func getHostKeyWindow() -> UIWindow? {
if #available(iOS 13, *) {
return windows.first { $0.isKeyWindow }
} else {
return keyWindow
}
}
And then simply make endEditing
as true
UIApplication.shared.getHostKeyWindow()?.endEditing(true)
It should close the keyboard in the window for which you have the reference!
How to dismiss keyboard when touch outside the textfield?
Try bellow code it hides the keyboard when user touches the view of uiviewcontroller, the code is in swift 3.0, hope it helps you.
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
self.view.endEditing(true) //This will hide the keyboard
}
Or else you have to set uitapgesturerecognizer for that specific view, or you can make that view uicontrol and set touchupinside event,
All you have to do is call self.view.endEditing(true) from any of above thing.
Dismiss Keyboard And Allow Button Gesture Recognizers Swift iOS
I fixed this using the following code:
I detect the location of the user's touch
I check if the touch is in the frame of each of the two buttons in my
UIView
If this is true, I manually call the button handler and return false from the method
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
if let view = self.view as? MyViewControllersView {
let location = touch.location(in: self.view)
if view.saveButton.frame.contains(location) {
view.saveHit()
return false
} else if view.scanButton.frame.contains(location) {
view.scanHit()
return false
}
}
return true
}
Related Topics
How to Mimic the Bottom Sheet from the Maps App
How to Correctly Linearize Depth in Opengl Es in Ios
Sending Data With Segue With Swift
How to Test Apple Push Notification Service Without an Iphone
How to Create .Ipa File Using Xcode
Trying to Convert Firebase Timestamp to Nsdate in Swift
Drawing Uibezierpath on Code Generated Uiview
Xcode 9 Gm - Wkwebview Nscoding Support Was Broken in Previous Versions
Xcode 7 Error: "Missing iOS Distribution Signing Identity For ..."
Detecting the Call Events in Ios
How to Support Universal Links in iOS App and Setup Server For It
Replacement For Deprecated -Sizewithfont:Constrainedtosize:Linebreakmode: in iOS 7
Uiview and Initwithframe and a Nib File. How to Get the Nib File Loaded