1

As below screenshot showed, I have a black dimmer View on the top and a viewController on the bottom(the white one) with half screen size.

What I want to do is when I tap on the dimmer place aka. outside of the viewController. This viewController should be dismissed.

YES, I found tons of topic using touchesBegan. But in my case it is not working, the problem is touchesBegan cannot detect touches outside current presented viewController. It would never be triggered when I click on that dimmer view.

And if I use Gesture check on the dimmer view, I could not dismiss this viewController from dimmer view too.

Any advice is welcomed if anyone met this similar situation with me.

enter image description here

Zhou Haibo
  • 1,681
  • 1
  • 12
  • 32

1 Answers1

0

You should be able to add a UITapGesture to the views window, look here: Dismiss modal view form sheet controller on outside tap

Ayrton CB
  • 454
  • 2
  • 9
  • Yes, I could add UITapGesture to my dimmer view. And I check this post, it is not clear for me that how could I connect this view to my modalViewController since I need to dismiss it. – Zhou Haibo Jun 29 '21 at 12:11
  • on your modalViewController you could add the gesture using `view.window?.add...` then when the tap Is recognised you can call `self.dismiss(animated: true, completion: nil)` – Ayrton CB Jun 29 '21 at 12:13
  • Thanks. I add gesture `view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))` in my modalViewController. The effect is same with implement `touchBegan`, it only could detect the touches inside this viewController, instead I need to detect touches outside it. – Zhou Haibo Jun 29 '21 at 12:23
  • use view.window? instead of just view - Sorry for the late reply – Ayrton CB Jun 29 '21 at 14:01
  • NP, I try `view.window?` too, but this time it could trigger the tapGesture, but all space inside or outside viewController would be detected. Because when specify the window, it means the whole screen, if it is worked in your side? – Zhou Haibo Jun 29 '21 at 15:34