0

I have a couple textViews in one cell in a table view controller and I am trying to dismiss the keyboard when you touch anywhere outside the keyboard. I've tried the touches began method but it didn't work. The text views are not transparent and have user interaction enabled.

class RegisterTableViewController: UITableViewController {
    override func viewDidLoad() {
      // set all text views delegate to self
    }

    // dismiss keyboard on touch
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touch")
        super.touchesBegan(touches, with: event)
        view.endEditing(true)
    }
}

extension RegisterTableViewController: UITextViewDelegate {
    func textViewDidBeginEditing(_ textView: UITextView) {
        textView.text = ""
    }
}

I'm new to swift and would appreciate any help!

Joan
  • 905
  • 2
  • 9
  • 15
  • What do you expect the user to touch in order to dismiss the keyboard? And what are you actually test-touching it? – El Tomato Dec 21 '19 at 03:12
  • Does this answer your question? [iOS - Dismiss keyboard when touching outside of UITextField](https://stackoverflow.com/questions/5306240/ios-dismiss-keyboard-when-touching-outside-of-uitextfield) – craft Dec 21 '19 at 03:35
  • The above code and touchesBegan method works when it's all UITextField but it doesn't work for UITextViews. When the user touches outside of the keyboard, the keyboard should be dismissed (view.endEditing). – Joan Dec 21 '19 at 04:07

2 Answers2

1
  • 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")
    }
    
Nayan Dave
  • 1,078
  • 1
  • 8
  • 30
  • I tried self.textview.endEditing(true) still doesn't work. I don't know if it is something about UITextView or the touchesBegan method. (https://forums.developer.apple.com/thread/91263) Is the problem textview inside of table view controller? – Joan Dec 21 '19 at 05:09
  • I have changed the code , try that and get back please !! – Nayan Dave Dec 21 '19 at 06:20
  • @Jon, Anytime !! and Thank You for approving may answer :) – Nayan Dave Dec 21 '19 at 06:53
1

You need to add Tap gesture recognizer inside your cell. Place all you text inputs in a UIView. make outlet of UIView inside cell. and than add this code in your cell.

@IBOutlet weak var myView: UIView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    let tap = UITapGestureRecognizer(target: self, action:    #selector(self.dismissKeyboard))
    self.myView.addGestureRecognizer(tap)
}

@objc func dismissKeyboard() {
    self.endEditing(true)
}
Zain
  • 153
  • 9