黑色背景屏幕,同时显示弹出视图,使用单独的 UIViewController

Black background screen, while showing a popup view, using a separate UIViewController

我有一个 UIViewController,它嵌入了一个 UINavigationController。在那个视图控制器中,我有一个 UIButton,它显示了一个弹出视图,这是另一个 UIViewController (pVC)。它通过设置为 showModallysegue 来实现。在弹出窗口中,我只有一个 UIView

现在,我将我的 pVC 的背景颜色设置为清除,因此当点击按钮时,它会产生类似只显示 pVC 内部视图的效果。

问题是,当我将 pVC 的背景颜色设置为 clear 并点击按钮时,pVC 按预期显示在我当前 view controller 的顶部,但一秒钟后,背景变黑。这是它的样子:

View Hierarchy Debugger 中,我看到这是一个 UIWindow 层变黑了。我想知道,这可能是因为我之前的 view controller 嵌入了 UINavigationController?当我尝试对两个 UIViewControllers 执行相同操作时,其中的 none 嵌入任何内容,它工作正常。

如果你知道发生这种情况的确切原因and/or如何解决这个问题,我将不胜感激你的帮助。

尝试在当前上下文中以模态方式显示 pVC,这样做:


关于overCurrentContext:

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.

改为将弹出视图的背景颜色更改为透明,然后像这样进行转场

还将此代码添加到显示弹出窗口的 viewController,并通过在情节提要中模态选择 with/without 动画

将 segue 的名称更改为您的名称
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if(segue.identifier == "showLogout")
    {

         let newVC = segue.destination;

        self.setPresentationStyleForSelfController(selfController: self,presentingController: newVC)


    }
  }

func setPresentationStyleForSelfController(selfController:UIViewController,presentingController:UIViewController)
{
   if #available(iOS 8.0, *)
    {

        //iOS 8.0 and above

        presentingController.providesPresentationContextTransitionStyle = true;

        presentingController.definesPresentationContext = true;

        presentingController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext




    }
    else
    {

         presentingController.modalPresentationStyle = UIModalPresentationStyle.currentContext

        selfController.navigationController?.modalPresentationStyle = UIModalPresentationStyle.currentContext
    }

}