Dismiss Keyboard with a Uitextview

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 your UITableViewCell file , which will work if you touch outside TextField but inside cell

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.Your_TextField.endEditing(true)
    }
  • But it won't work outside cell (In UIVIew of another ViewController) , so add UITapGesture to achieve that

    override 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



Leave a reply



Submit