Hide keyboard when scroll UITableView

前端 未结 8 2253
借酒劲吻你
借酒劲吻你 2020-12-02 05:47

In my app i want hide keyboard when i start scrolling UITableView. I search about this in internet, and most answer is subclassing UITableView (http://stackoverflow.com/ques

8条回答
  •  没有蜡笔的小新
    2020-12-02 05:59

    With Swift 5

    To hide the keyboard when scrolling the TableView and stop editing properly, we still need to combine two types of answers:

    1. Set the keyboard dismiss mode in IB (as Kyle explained) or in ViewDidLoad() code (as Pei explained) for instance:
    tableView.keyboardDismissMode = .onDrag
    
    1. Force the current textfield to resign as first responder (as in Vasily's answer). We just need to add the following to our UITableViewController class
        override func scrollViewDidScroll(_ scrollView: UIScrollView) {
            if !tableView.isDecelerating {
                view.endEditing(true)
            }
        }
    

提交回复
热议问题