我想在任何其他视图,甚至是导航栏上,显示一种“弹出”视图,看起来像这样:

全屏黑色背景,0.5 alpha,可以看到下面的另一个UIViewController。 中间有一个带有一些信息的UIView窗口,(如果你想知道一切,一个日历)。

为了做到这一点,我已经创建了一个UIViewController,它包含了两个uiview (background和window),我试图显示它。我尝试了一个简单的[mySuperVC addSubview:myPopUpVC。视图],但我仍然有上面的导航栏。

我试着把它表示为一个模态,但是下面的UIViewController消失了,我失去了透明效果。

有什么办法可以做到这一点吗,我相信这很简单……

谢谢!


当前回答

UIApplication.shared.keyWindow?.insertSubview(yourView, at: 1)

这个方法适用于xcode 9.4, iOS 11.4

其他回答

[self.navigationController。视图addSubview overlayView):;是你真正想要的吗

UIApplication.shared.keyWindow?.insertSubview(yourView, at: 1)

这个方法适用于xcode 9.4, iOS 11.4

做这件事的方法不止一种:

1-在UIWindow上添加你的UIView而不是在UIViewController上添加。这样的话,它就会在一切之上。

   [[(AppDelegate *)[UIApplication sharedApplication].delegate window] addSubview:view];

2-使用自定义过渡,将保持你的UIViewController显示在后面的0.5 alpha

为此,我建议你看看这个:https://github.com/Citrrus/BlurryModalSegue

注意,如果你想在全屏中添加视图,那么只能使用下面的代码

添加这些UIViewController的扩展

public extension UIViewController {
   internal func makeViewAsFullScreen() {
      var viewFrame:CGRect = self.view.frame
      if viewFrame.origin.y > 0 || viewFrame.origin.x > 0 {
        self.view.frame = UIScreen.main.bounds
      }
   }
}

继续正常的子视图添加过程

现在用于添加UIViewController的viewDidAppear

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

     self.makeViewAsFullScreen()
}

@Nicolas Bonnet回答的快速版本:

    var popupWindow: UIWindow?

func showViewController(controller: UIViewController) {
    self.popupWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
    controller.view.frame = self.popupWindow!.bounds
    self.popupWindow!.rootViewController = controller
    self.popupWindow!.makeKeyAndVisible()
}

func viewControllerDidRemove() {
    self.popupWindow?.removeFromSuperview()
    self.popupWindow = nil
}

不要忘记,窗口必须是一个强属性,因为最初的答案会导致立即释放窗口