在所有视图之上添加一个 UIView,甚至是导航栏

210
我想要显示一个"弹出窗口"视图,它在任何其他视图(甚至导航栏)之上,看起来像这样:
  • 全屏黑色背景,透明度为0.5,可以看到其他UIViewController下面的内容.
  • 一个位于中间的UIView窗口,其中包含一些信息(如果你想知道所有的内容,这是一个日历)。
为了实现这个,我创建了一个包含两个UIViews(背景和窗口)的UIViewController,并试图显示它。我尝试了简单的[mySuperVC addSubview:myPopUpVC.view],但我还是有导航栏在上面。
我试图将其呈现为模态视图,但下面的UIViewController消失了,我失去了透明效果。
有什么办法可以做到这一点,我相信它很简单...
谢谢!

在您的应用程序窗口上添加一个子视图。处理方向更改可能会很棘手:https://dev59.com/Ymoy5IYBdhLWcg3wF6QL - Amar
17个回答

4
[[UIApplication sharedApplication].windows.lastObject addSubview:myView];

3
在Swift 4.2和Xcode 10中
var spinnerView: UIView? //This is your view

spinnerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
//Based on your requirement change width and height like self.view.bounds.size.width
spinnerView?.backgroundColor = UIColor.black.withAlphaComponent(0.6)
//        self.view.addSubview(spinnerView)
let currentWindow: UIWindow? = UIApplication.shared.keyWindow
currentWindow?.addSubview(spinnerView!)

在Objective C中:
UIView *spinnerView;//这是你的视图
self.spinnerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];  
//Based on your requirement change width and height like self.view.bounds.size.width
self.spinnerView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
// [self.view addSubview:self.spinnerView];
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:self.spinnerView];

这可以在纵向或横向模式下运行。 另一个简单的代码是:
yourViewName.layer.zPosition = 1//Change your view name

2

注意:如果您想要在全屏模式下添加视图,则只需使用以下代码。

将这些扩展添加到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()
}

1
我会使用一个UIViewController子类,其中包含一个“容器视图”,该视图嵌入您的其他视图控制器。 然后,您将能够在Container View中添加导航控制器(例如使用嵌入关系segue)。
请参见实现容器视图控制器

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

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


0
[self.navigationController.navigationBar.layer setZPosition:-0.1];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 20, 35, 35)];
[view setBackgroundColor:[UIColor redColor]];
[self.navigationController.view addSubview:view];
[self.navigationController.view bringSubviewToFront:view];
self.navigationController.view.clipsToBounds = NO;
[self.navigationController.navigationBar.layer setZPosition:0.0];

enter image description here


0
您需要向具有UITextEffectsWindow类型的第一个窗口添加子视图。要添加到第一个窗口,因为自定义键盘会添加它们的UITextEffectsWindow,如果您向其添加子视图,则不会正常工作。 此外,您不能将子视图添加到最后一个窗口,因为例如键盘也是一个窗口,您无法从键盘窗口中呈现。 因此,我找到的最佳解决方案是将子视图(甚至推送视图控制器)添加到具有UITextEffectsWindow类型的第一个窗口中,该窗口覆盖辅助视图、导航栏-所有内容。
let myView = MyView()
myView.frame = UIScreen.main.bounds

guard let textEffectsWindow = NSClassFromString("UITextEffectsWindow") else { return }
let window = UIApplication.shared.windows.first { window in
    window.isKind(of: textEffectsWindow)
}
window?.rootViewController?.view.addSubview(myView)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接