Ios – Black screen after presenting modal view controller in current context from UITabBarController

iosiphoneobjective cuikit

My root view controller is a UITabBarController. I'm trying to present a modal view controller over one of the tab bar controller's view controllers, but still allow the tab bar to be used to go to a different tab – in other words, I would like for the modal to only interrupt to the flow of that particular tab, not the entire app.

To do this, I've set the presenting view controller's presentation style as 'Over Current Context' in storyboard. The problem I'm having is that after presenting the modal view controller and selecting a new tab, the presenting view controller's view is removed from the window, and isn't added back to the window when dismissing the presented view controller. After dismissing the view controller, moving to another tab and then coming back finally puts the presenting view controller back into the window.

I've reproduced my problem using the 'Tabbed' template in Xcode.

After presenting modal – I've added transparency to the presented view controller to easily see what's going on in the presented view controller.

Changing to second tab and then back – the presenting view controller's view has now been removed.

Dismissing the modal leaves the presenting view controller with it's view still removed from the window. Going to tab 2 and returning adds the view back to the window.

I'm hoping this is something simple I've overlooked in storyboard, but the fact that I can present the modal and see the presented view controller behind it before changing tabs makes me think I have things set up correctly.

Best Answer

I had the same issue and was able to solve it by setting self.definesPresentationContext = YES; on the presenting view controller before presenting the modal VC. You can also set this in a storyboard, the checkbox is called "Defines Context" in Interface Builder.