如我們所知,我們使用下面的代碼可以得到endEditing
這個searchBar的firstResponder,但是如果有scrollView或tableView的話,效果是不同的。添加tapGesture到tableView然後不能執行tableView(_tableView:UITableView,didSelectRowAt indexPath:IndexPath)方法
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.view.endEditing(true)
}
我添加tapGesture
到的tableView,這樣我就可以endEditing
搜索欄的firstResponder。
但在將tapGesture
添加到我的tableView之後,tableView的tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
函數將不再起作用。
我該如何解決這個問題?
加成
我有用的代碼如下:
let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapTableView))
self.tableView.addGestureRecognizer(tap)
func tapTableView() {
self.searchBar.endEditing(true)
}
除非你檢查它觸碰它會覆蓋全觸控的觀點,而不是你可以考慮使用內置在諸如薛志東隋衛平李利軍 – SeanLintern88
@ SeanLintern88如何在tableview中的keyboardDismiss你的意思是在tableView上使用kwyboardDismiss? – aircraft