hide keyboard for text field in swift programming language
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.
Hide Keyboard on Textfield when it Editing
1.Set the textField delegate like below image
(A)
(B)
2.Add textField delegate methods
Swift
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// Implement your Date Time Picker initial Code here
return false
}
Objective C
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
// Implement your Date Time Picker initial Code here
return NO;
}
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()
Related Topics
Adding Blur Effect to Background in Swift
Uipangesturerecognizer - Only Vertical or Horizontal
How to Create a Big, Red Uibutton with iOS
Cocoapods: Failed to Connect to Github to Update the Cocoapods/Specs Specs Repo
Pod Install Is Staying on "Setting Up Cocoapods Master Repo"
What's the Uiscrollview Contentinset Property For
How to Check If a File Exists in Documents Folder
Uicollectionview Spacing Margins
Pull to Refresh Uitableview Without Uitableviewcontroller
What Exactly Can Corebluetooth Applications Do Whilst in the Background
Add a Uiview Above All, Even the Navigation Bar
How to Use Presentmodalviewcontroller to Create a Transparent View
Changing Placeholder Text Color with Swift