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






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



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

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế