NB: My gesture recognizers are not different and are in different viewControllers.
TLDR: How can I do to have both my tap gesture recognisers to work?
I have created a side menu from some container views and I added to it a gesture recognizer that allows me to dismiss it on tap. Then I created another ViewController where i have some textfields. Since I wanted the keyboard to dismiss on tap I even added a gesture recognizer that allows me to hide the keyboard whenever i tap the view. Now I noticed that having the gesture that hides the keyboard doesn't let my side menu hide on tap:
SIDE MENU VIEW CONTROLLER
@IBOutlet weak var bigContainer: UIView!
@IBOutlet weak var sideMenuConstraint: NSLayoutConstraint!
var sideMenuOpen = false
var gesture : UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(toggleSideMenu), name: NSNotification.Name("ToggleSideMenu"), object: nil)
gesture = UITapGestureRecognizer(target: self, action: #selector(ContainerViewController.toggleSideMenu))
}
@objc func toggleSideMenu() {
if sideMenuOpen {
sideMenuOpen = false
sideMenuConstraint.constant = -240
self.bigContainer.removeGestureRecognizer(gesture!)
} else {
sideMenuOpen = true
sideMenuConstraint.constant = 0
self.bigContainer.addGestureRecognizer(gesture!)
}
}
MAIN VIEW VIEW CONTROLLER
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var menuOutlet: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
let endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false
view.addGestureRecognizer(endEditingTapGesture)
}
@IBAction func toggleSideMenu(_ sender: Any) {
print("Toggle side menu")
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
}
}
I tried removing the tap gesture to hide the keyboard in my toggleSideMenu
button but it didn't work.
In your MAIN VIEW CONTROLLER Replace with below code:
var endEditingTapGesture:UIGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
endEditingTapGesture = UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))
endEditingTapGesture.cancelsTouchesInView = false
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notication:)), name: UIResponder.keyboardWillHideNotification, object: nil) //Add keyboard notification
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notication:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@IBAction func toggleSideMenu(_ sender: Any) {
print("Toggle side menu")
NotificationCenter.default.post(name: NSNotification.Name("ToggleSideMenu"), object: nil)
}
//Keyboard method
@objc func keyboardWillHide(notication:Notification) {
view.removeGestureRecognizer(endEditingTapGesture)
}
@objc func keyboardWillShow(notication:Notification) {
view.addGestureRecognizer(endEditingTapGesture)
}