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
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<UITouch>, 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 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()
iOS - Dismiss keyboard when touching outside of UITextField
You'll need to add an UITapGestureRecogniser
and assign it to the view, and then call resign first responder on the UITextField
on it's selector.
The code:
In viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
In dismissKeyboard:
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
}
(Where aTextField
is the textfield that is responsible for the keyboard)
Swift 3 version looks like that
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:)))
self.view.addGestureRecognizer(tapGesture)
For dismissKeyboard
@objc func dismissKeyboard (_ sender: UITapGestureRecognizer) {
aTextField.resignFirstResponder()
}
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 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
Best way to dismiss keyboard when tapping outside of UITextField - IOS
just do this:
class viewController: UIViewController, UITextFieldDelegate {
// Outlets
@IBOutlet weak var logoImage: UIImageView!
@IBOutlet weak var nameTF: CustomTextField!
@IBOutlet weak var emailTF: CustomTextField!
@IBOutlet weak var passwordTF: CustomTextField!
@IBOutlet weak var confirmPassTF: CustomTextField!
// Actions
@IBAction func signupButton(_ sender: UIButton) {
}
override func viewDidLoad() {
super.viewDidLoad()
logoImage.image = UIImage(named: "logo2")
nameTF.delegate = self
emailTF.delegate = self
passwordTF.delegate = self
confirmPassTF.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dissMissKeyboard))
view.addGestureRecognizer(tap)
}
func dissMissKeyboard() {
view.endEditing(true)
}
Can't dismiss keyboard when editing UITextView and touching anywhere outside of keyboard in table view controller
Add
touchesBegan
code in yourUITableViewCell
file , which will work if you touch outsideTextField
but inside celloverride func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.Your_TextField.endEditing(true)
}But it won't work outside
cell
(InUIVIew
of anotherViewController
) , so addUITapGesture
to achieve thatoverride func viewDidLoad() {
super.viewDidLoad()
let tapgest = UITapGestureRecognizer(target: self, action: #selector(taptoend))
self.Your_Table_View.addGestureRecognizer(tapgest)
}
@objc func taptoend()
{
self.Your_Table_View.endEditing(true)
print("Key-Board will be dismissed here")
}
How to exit keyboard on tap outside of uitextfield or button
You could exclude the taps on subviews using gestureRecognizer(_:, shouldReceive:)
method in UIGestureRecognizerDelegate
.
extension UIViewController: UIGestureRecognizerDelegate {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
tap.cancelsTouchesInView = false
tap.delegate = self
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
view.endEditing(true)
}
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
touch.view?.isDescendant(of: view) == false // will return false if touch was received by a subview
}
}
Update: You could use touch.view == view
instead of touch.view?.isDescendant(of: view) == false
.
Related Topics
How to Record a Conversation/Phone Call on Ios
-Didselectrowatindexpath: Not Being Called
How to Change Uipickerview Height
Get Location Updates For iOS App Even When Suspended
How to Write a File to a Folder Located At Apple'S Files App in Swift
Iphone Navigation Bar Title Text Color
Multiple Lines of Text in Uilabel
Creating a Uicollectionview Programmatically
How to Create a Button Programmatically
How to Hide Keyboard When Using Swiftui
How to Add Text to an Image in iOS Swift
Ios App Submission: Missing 64-Bit Support
How to Download Multiple Files Sequentially Using Nsurlsession Downloadtask in Swift
Passing Data With Unwind Segue