我在我的ViewController中向我的主UITapGestureRecognizer
添加了Content View
,以便在点按内容视图时关闭我的键盘。
问题是我的内容视图中有UICollectionView
,并设置UITapGestureRecognizer
拦截了UICollectionView
的点按。
如何允许我的UICollectionView
点按,以便didSelectItemAtIndexPath
方法再次触发?
func setupGestureRecognizer() {
let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
contentView.addGestureRecognizer(dismissKeyboardTap)
}
func dismissKeyboard() {
contentView.endEditing(true)
}
答案 0 :(得分:16)
解决此问题的方法是在您的.cancelsTouchesInView = false
中添加 UITapGestureRecognizer
。
这允许其他视图中的触摸通过,例如UITableViewCell
触摸。
func setupGestureRecognizer() {
let dismissKeyboardTap = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
dismissKeyboardTap.cancelsTouchesInView = false
contentView.addGestureRecognizer(dismissKeyboardTap)
}
func dismissKeyboard() {
contentView.endEditing(true)
}
答案 1 :(得分:0)
试试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
并删除你的tapGesture。