弹出窗口即使设置为清除也具有黑色背景

时间:2018-09-18 19:04:05

标签: swift xcode uinavigationcontroller popup presentmodalviewcontroller

我有一个UIViewController,我要在另一个上方展示。它应该显示为带有透明背景的弹出窗口。它正常显示,但背景为黑色。嵌入UINavigationController中时显示。

问题是背景颜色是黑色,即使所有背景颜色都设置为清除,我也看不到下面的视图。我什至尝试在介绍控制器时设置背景清晰,但仍然一无所获。我在Storyboard中设置了它,以显示当前上下文并交叉分解。

视图调试器也不会在层次结构中将任何视图显示为黑色。不知道出什么问题了,感谢您的帮助,谢谢!

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 个答案:

答案 0 :(得分: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.

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.

答案 1 :(得分:0)

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)