title: Swift: Dismissing Modally Presented View Controller when Tab Bar is tapped
body: <p>I have a tabbed application with a subclassed UITabBarController. My first view controller is capable of modally presenting another view controller.</p>
  
  <p>My modally-presented view controller is supposed to dismiss itself when anything outside its content is tapped. The code responsible is as follows:</p>
  
  <pre><code>override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      let touch : UITouch? = touches.first
      if (touch?.view != contentView){
          self.dismiss(animated: true, completion: nil)
      }
  }
  </code></pre>
  
  <p>If I tap on the background outside the view, the modally-presented view controller dismisses as expected. However, it doesn't dismiss as expected if I tap on my Tab Bar.</p>
  
  <p>Furthermore, if my modal view controller hasn't been dismissed and I go to another view controller using my tab bar, when I go back to my first tab, I'm greeted with my modal view controller over a black background (in other words, when I dismiss the modal view controller in this situation, all that remains is a black void and the tab bar controller. I'm then forced to select another tab, then select the first tab again in order to fix it).</p>
  
  <p>Any ideas about how to fix this/avoid this behavior? I'm wondering if there is, perhaps, a way for my modally-presented view to "pick up" on my tab bar being tapped on so it can dismiss itself (or if there's another, better way), but after searching for a while I haven't found any solutions.</p>