Popup has black background even though it is set to clear

Viewed 2683

I have a UIViewController that I'm presenting over another. It should show as a popup with a transparent background. It shows normally, but the background is black. It is presented while embedded in a UINavigationController.

The problem is that the background color is black and I can't see the below view, even though all backgrounds colors are set to clear. I even tried setting the backgrounds clear on presenting the controller and still nothing. I have it set in the Storyboard to show over current context and cross dissolve.

The view debugger does not show any of the views as black in the hierarchy either. Not sure what is wrong, any help is appreciated, thanks!

let vc = UIStoryboard.init(name: "AccountSettings", bundle: nil).instantiateViewController(withIdentifier: "upgradeVCPopup") as! UpgradeVCPopup
let nav = UINavigationController(rootViewController: vc)
nav.view.backgroundColor = .clear
vc.view.backgroundColor = .clear
if let navigationController = self.navigationController {
    navigationController.present(nav, animated: true, completion: nil)
} else {
    self.present(nav, animated: true, completion: nil)
}
2 Answers

The reason you have a black background is that you did not set your UIViewControllers UIModalPresentationStyle. This parameter determines how is your modal view controller displayed. The default value for modal view controllers is fullScreen.

From Apple Documentation:

UIModalPresentationStyle.fullScreen

The views belonging to the presenting view controller are removed after the presentation completes.

This means that after your view controller is presented, the previous view controller is removed. And when it's removed, since there is nothing below it, you see a black color. If you want previous view controller to stay and be visible you may set modalPresentationStyle to overFullScreen.

From Apple Documentation:

UIModalPresentationStyle.overFullScreen

The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.

To make that, add the following line before presenting your view controller:

vc.modalPresentationStyle = .overFullScreen

This should help.

Have you tried setting the nav.view.backgroundColor = .white By setting the nav.view.backgroundColor = .clear you are setting the background of the Navigation window to clear (default base of the Nav window is black)

Related