简体   繁体   中英

How to dismiss keyboard in a tableView after typing in textview

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

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.

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

关闭键盘属性
Here you can set property as you want

or if you want via programming then

self.tableView.keyboardDismissMode = .onDrag

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM