문제 설명
UITableViewCell에서 키보드 탭을 숨기는 UITapGestureRecognizer (UITapGestureRecognizer to hide keyboard taps on UITableViewCell)
아래 스크립트가 있으며 키보드가 켜져 있을 때 이 UIController에서 주변을 클릭하면 키보드가 숨겨지지만 셀도 클릭됩니다. 나는 그런 일이 일어나기를 원하지 않습니다.
cancelsTouchesInView
가 그 기능을 제어한다는 것을 알고 있습니다... 그리고 이것을 true로 설정하면 셀을 클릭하지 않지만 키보드가 숨겨지면 셀을 클릭할 수 없습니다.
이에 대한 좋은 해결책이 있습니까?
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard2))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard2() {
view.endEditing(true)
}
}
</code></pre>
참조 솔루션
방법 1:
Your best bet may be to disable user interaction on your tableView when the keyboard is shown, and then re‑enable it when you end editing.
However, you might give this a try and see if it does what you need.
Change your signature for the dismissKeyboard2
func to get a reference to the tap gesture recognizer and remove it when you end editing:
extension UIViewController {
func hideKeyboardWhenTappedAround() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard2(_:)))
tapGesture.cancelsTouchesInView = true
view.addGestureRecognizer(tapGesture)
}
@objc func dismissKeyboard2(_ g: UITapGestureRecognizer) {
view.endEditing(true)
view.removeGestureRecognizer(g)
}
}
참조 문서