Popup has black background even though it is set to clear
Popup has black background even though it is set to clear
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
.
UIViewController
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.
Storyboard
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
2
The reason you have a black background is that you did not set your UIViewController
s UIModalPresentationStyle
. This parameter determines how is your modal view controller displayed. The default value for modal view controllers is fullScreen
.
UIViewController
UIModalPresentationStyle
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
.
modalPresentationStyle
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)
nav.view.backgroundColor = .white
nav.view.backgroundColor = .clear
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
This was it, thanks! I had that correctly set on the VC in the storyboard, and completely didn't realize that I was missing it for the nav, since I was creating that programattically.
– Michael
Sep 18 '18 at 20:22