UITableViewCell에서 키보드 탭을 숨기는 UITapGestureRecognizer (UITapGestureRecognizer to hide keyboard taps on UITableViewCell)


문제 설명

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

(by letsCodeDonMag)

참조 문서

  1. UITapGestureRecognizer to hide keyboard taps on UITableViewCell (CC BY‑SA 2.5/3.0/4.0)

#uitableview #uitapgesturerecognizer #swift






관련 질문

레코드 삭제 중 오류 발생, 포착되지 않은 예외 'NSInternalInconsistencyException'으로 인해 앱 종료 (error while deleting records,Terminating app due to uncaught exception 'NSInternalInconsistencyException')

IOS 7에서 편집 상태의 셀을 반환하는 dequeueReusableCellWithIdentifier (dequeueReusableCellWithIdentifier returning cells in editing state in IOS 7)

UITableViewCell 탭 호버 (UITableViewCell tap hover)

ios swift tableview 셀 사이의 공간을 어떻게 변경할 수 있습니까? (How can I change the space between tableview cells ios swift)

테이블 뷰 셀을 선택할 때 뷰 컨트롤러를 푸시할 수 없습니다. (Can't push my view controller when I select a table view cell)

셀이 선택되면 UIImageView가 사라지지만 UILabel은 사라지지 않습니다. (UIImageView disappears but not UILabel when cell is selcted)

UITableView의 테두리를 설정하는 방법은 무엇입니까? (How to set a border for UITableView?)

plist에서 특정 데이터를 가져오는 UITableView(탐색 템플릿을 사용하지 않음) (UITableView (not using Navigation Template) getting specific data from plist)

searchBar의 일반 텍스트 버튼에 액세스하는 방법은 무엇입니까? (How to access searchBar's clear text button?)

'NSInternalInconsistencyException', 이유: '범위를 벗어난 섹션(0)에 대한 행 수를 요청했습니다.' ('NSInternalInconsistencyException', reason: 'Requested the number of rows for section (0) which is out of bounds.')

UITableViewCell에서 키보드 탭을 숨기는 UITapGestureRecognizer (UITapGestureRecognizer to hide keyboard taps on UITableViewCell)

특정 UITableViewCells에 고유한 삽입을 추가하는 방법은 무엇입니까? (How to add unique insets to specific UITableViewCells?)







코멘트