UICollectionViewが埋め込まれたUITableViewがあります。最初のセクションにはUITextFieldのカスタムセルがあり、2番目のセクションにはUICollectionViewがあります。UITableViewのUICollectionViewは、view.addGestureRecognizer()のためにタップを検出しません。
ユーザーはそれのために私が
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)
func dismissKeyboard(sender: UIGestureRecognizer) {
view.endEditing(true)
}
を使用していますUITextFieldの外でタップしたときに、キーボードを非表示にするには、私が考え出してきたように、私のUICollectionViewは、その項目のタップを認識しないこと。削除した場合view.addGestureRecognizer(タップ)すべて正常に動作しますが、キーボードはこの場合は非表示になりません。
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder() //if desired
return true
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell", for: indexPath) as! ProfileTableViewCell
cell.value.delegate = self // value is my textField
return cell
case 1:
let cell = tableView.dequeueReusableCell(withIdentifier: "WallpaperTableViewCell", for: indexPath) as! WallpaperTableViewCell
cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, index: (indexPath as NSIndexPath).row)
return cell
default:
let cell = UITableViewCell()
return cell
}
}
どのようにこの問題を修正し、隠れキーボードを一緒に使用し、UICollectionViewで認識をタップすることができますか? 感謝のUITableViewで、私はフィリップ・ミルズ
にhttps://github.com/DahanHu/DHCollectionTableView
UPD
おかげで、この例を使用していたUICollectionViewを埋め込むには
は、答えはかなりの最も簡単な
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
と
ましたfunc keyboardWillShow(notification: NSNotification) {
view.addGestureRecognizer(tap)
}
func keyboardWillHide(notification: NSNotification) {
view.removeGestureRecognizer(tap)
}