のUIViewのサブビューにタッチイベントを無効にするために、あなたはUIViewController
に、この拡張機能を追加することができます。
extension UIViewController:UIGestureRecognizerDelegate {
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
tap.delegate = self
}
func dismissKeyboard() {
view.endEditing(true)
}
public func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
if (touch.view!.isDescendantOfView(self.view) && touch.view != self.view){
return false
}
return true
}
}