0
votes

I have a tabbed application with a subclassed UITabBarController. My first view controller is capable of modally presenting another view controller.

My modally-presented view controller is supposed to dismiss itself when anything outside its content is tapped. The code responsible is as follows:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch : UITouch? = touches.first
    if (touch?.view != contentView){
        self.dismiss(animated: true, completion: nil)
    }
}

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.

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).

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.

1

1 Answers

1
votes

For dismissing a modally presented viewController,

you can add a action on Tabbar button item,

and then just use,

self.dismiss(animated: true, completion: nil)