Search code examples
iosswift4

Dismiss UIView when tapped outside the view


I have a button say buttonA in my one of my tableview cells which when tapped displays a view say view1. View1 dismisses only when I tap on the button. However I want that view1 to be dismissed when I tap outside the view.

I have tried with :

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissDropdownView))
        self.view.addGestureRecognizer(tapGesture)

.

@objc func dismissDropdownView() {
        dropDownView.isHidden = true
    }

The issue I'm facing with the above is that when I tap on other buttons(other than buttonA) in my tableview cell, they do not perform required action.

How can I fix this?


Solution

  • Set the cancelsTouchesInView property of your UITapGestureRecognizer to false.