How to dismiss keyboard iOS programmatically when pressing return
The simple way is to connect the delegate of UITextField
to self (self.mytestField.delegate = self
) and dismiss the keyboard in the method textFieldShouldReturn
using [textField resignFirstResponder];
Another way to dismiss the keyboard is the following:
Objective-C
[self.view endEditing:YES];
Swift:
self.view.endEditing(true)
Put [self.view endEditing:YES];
where you would like to dismiss the keyboard (Button event, Touch event, etc.).
dismiss keyboard from all view hierarchy programmatically in swift
If you're trying to dismiss keyboard in UIViewController
try:
@objc func dismissKeyboard() {
view.endEditing(true)
resignFirstResponder()
}
Dismiss keyboard through return key with swift
Try setting the delegate. Add a didSet to the text outlet. Like this.
@IBOutlet var text: UITextField! {
didSet {
text.delegate = self
}
}
Dismissing the keyboard when a button is pressed, programmatically with swift?
It all depends on what triggered the keyboard to appear in the first place. If you have a UITextField
on your page that brought it up, you can call textField.resignFirstResponder()
to hide the keyboard again.
If you used some other object to bring up the keyboard, simply take the reference of whatever you used and call resignFirstResponder()
on that object.
Example:
Lets say you are using a button button1
to close the keyboard, and you have a textField1
that is triggering the keyboard.
button1.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "buttonTapped"))
Then in your buttonTappedFunction
func buttonTapped(){
textField1.resignFirstResponder()
}
How do i dismiss the keyboard on return key from a UITextView
Remember to add the UITextDelegate into ViewDidLoad()
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n"
{
textView.resignFirstResponder()
return false
}
return true
}
Hide keyboard by pressing button
[self.view endEditing:YES]
Paste it in action handler of your controller.
Or for Swift:
self.view.endEditing(true)
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
Related Topics
Retain Cycle (Strong Reference) Fix for Custom Uitextfield
Mobile Safari Position:Fixed Z-Index Glitch When Scrolling
Nsdateformatter: Date According to Currentlocale, Without Year
Getting the Value of a Uitextfield as Keystrokes Are Entered
Warning: Attempt to Present * on * Whose View Is Not in the Window Hierarchy - Swift
When Does a Uitableview's Contentsize Get Set
Wireless iPhone App Distribution - Problem with Itms-Services Protocol
iOS 7 - How to Disable the Swipe Back and Forward Functionality in Safari
Getter VS Computed Property. What Would Warrant Using One of These Approaches Over the Other
Webkit Overflow Scrolling Touch CSS Bug on iPad
Catch an Exception for Invalid User Input in Swift
Uiimageview Missing Images in Launch Screen on Device
iPhone Sdk:How to Play Video Inside a View? Rather Than Fullscreen
Add Shadow on Uiview Using Swift 3