3

In my app I have a UITableView, which includes in its first cell a UITextView and in the other cells just UILabels.

How can I dismiss the keyboard after typing something in the UITableView? I want to dismiss it anytime I tap on the other cells or scroll the tableview.

Sufian
  • 6,405
  • 16
  • 66
  • 120
White Hat
  • 681
  • 1
  • 6
  • 15
  • possible duplicate of [How to dismiss keyboard for UITextView with return key?](http://stackoverflow.com/questions/703754/how-to-dismiss-keyboard-for-uitextview-with-return-key) – Adrian Aug 31 '15 at 00:38
  • no it's not the same question. Return key in UITextView is for new line and I want it that way. I need to dismiss the keyboard when I start scrolling the UITableView or when tapping on anywhere outside the UITextView. – White Hat Aug 31 '15 at 01:14
  • http://stackoverflow.com/a/4132774/4475605 – Adrian Aug 31 '15 at 01:17

2 Answers2

6

Here is the easiest and simplest way How I am doing this via IB.

Dismiss keyboard property
Here you can set property as you want

or if you want via programming then

self.tableView.keyboardDismissMode = .onDrag
Mrugesh Tank
  • 3,495
  • 2
  • 29
  • 59
5

You can use the UITableViewDelegate which conforms to the UIScrollViewDelegate to implement:

func scrollViewWillBeginDragging(scrollView: UIScrollView) {
     dismissKeyboard()
}

func dismissKeyboard(){
     self.view.endEditing(true)
}

//Add to viewDidLoad:
var tapGesture = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
tableView.addGestureRecognizer(tapGesture)

//Or since you wanted to dismiss when another cell is selected use:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
     dismissKeyboard()
}
keji
  • 5,947
  • 3
  • 31
  • 47