How to dismiss keyboard for UITextView with return key?
UITextView
does not have any methods which will be called when the user hits the return key. If you want the user to be able to add only one line of text, use a UITextField
. Hitting the return and hiding the keyboard for a UITextView
does not follow the interface guidelines.
Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText:
method of UITextViewDelegate
and in that check if the replacement text is \n
, hide the keyboard.
There might be other ways but I am not aware of any.
dismiss keyboard with a uiTextView
This works for me:
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
/* Updated for Swift 4 */
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
/* Older versions of Swift */
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
textView.resignFirstResponder()
return false
}
return true
}
}
UITextView - Keyboard Dismiss on Drag or Dismiss Interactively
For the built-in keyboard dismissing to work for a UIScollView
or one of its subclasses (e.g. UITextView
) the scroll view needs to be able to scroll. If there's not enough text in the text view to provide for scrolling, it won't perform a keyboard dismiss.
However, you can turn on vertical bouncing and then it will work. Check "Bounce Vertically" in interface builder or in code set myTextView.alwaysBounceVertical = true
.
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
}
Swift Dismiss UITextView Keyboard
You have to set the textview.delegate
to self and use the shouldChangeTextInRange
function to resign on pressing return.
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
if text == "\n" // Recognizes enter key in keyboard
{
textView.resignFirstResponder()
return false
}
return true
}
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
}
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")
}
Dismiss Keyboard for textview
You have made everything correct, but there is one step missing.
In order to hide or show the keyboard via the responder you have to set the delegate otherwise it won't work.
You can do it for example in the viewDidLoad:
override func viewDidLoad() {
myTextView.delegate = self
}
Related Topics
What Are "Intervals" in Swift Ranges
Scanning Real-World Object and Generating 3D Mesh from It
Alamofire - Nsurlcache Is Not Working
What Does the Swiftui '@State' Keyword Do
Swift: What Does Backslash Dot "\." Mean
The "Prefs" Url Scheme Is Not Working in iOS 10 (Beta 1 & 2)
Swift - Increment Label With Stepper in Tableview Cell
Check If Key Exists in Dictionary of Type [Type:Type]
Array Element Cannot Be Bridged to Objective-C
Swift 5: What's 'Escaping Closure Captures Mutating 'Self' Parameter' and How to Fix It
Why Is Manually Setup Root View Controller Showing Black Screen
Making Simple Accordion Tableview in Swift
Swift & Firebase | Checking If a User Exists with a Username
Swift Repl: How to Import, Load, Evaluate, or Require a .Swift File
Swift 2.0: Protocol Extensions: Two Protocols with the Same Function Signature Compile Error
Swiftui - Navigation Bar Button Not Clickable After Sheet Has Been Presented