Black screen after presenting modal view controller in current context from UITabBarController
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.
Solution 1:
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.
Solution 2:
Try to set your presentation style as 'Over Full Screen' instead of 'Over Current Context' in storyboard.
Solution 3:
iOS 10+ & Swift 3+
I've very nice solution of this problem. Use, over full screen modal presentation style for a view controller, is being presented.
let storyboard = UIStoryboard(name: "Main", bundle: nil) // Replace “Main” with your storyboard name
if let viewController = storyboard?.instantiateViewController(withIdentifier: “viewController Identifier”) as? ViewController {
viewController.modalPresentationStyle = .overFullScreen
self.present(viewController, animated: false, completion: {
})
}
Over full screen will present your view controller over your tabbar (controller) by covering it. So, end-user can't switch tabbar (unless you perform operation programatically) tabbar items. User must close this view controller to switch tabbar.
If you are using segue, to present view controller then select 'Over Full Screen' from modal presentation style in Attribute inspection
Solution 4:
i had this problem : when i present my ModalController it takes transparent background and when i changed the tab to next one in tabBarController and getBack to previous one the transparent background was gone and and there is a bad black background after research i found the point the point is this:
self.definesPresentationContext = true
the self is not modal controller self is that presenting controller for modalController and another point is .overCurrentContext like this
self.definesPresentationContext = true
modalController.modalPresentationStyle = .overCurrentContext
self.present(modalController, animated: true, completion: nil)
Solution 5:
Try presenting the view controller in application window. I had a similar problem which was fixed by below code:
let myNewVC = mainStoryBoard.instantiateViewController(withIdentifier: "MyNewVCId") as! MyNewVC
let navController = UINavigationController(rootViewController: myNewVC)
navController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
let appDelegate = UIApplication.shared.delegate as? AppDelegate
appDelegate?.window?.rootViewController?.present(navController, animated: true, completion: nil)
Hope this helps you too.