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

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

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

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

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

谢谢!


你可以通过将你的视图直接添加到keyWindow来实现:

UIView *myView = /* <- Your custom view */;
UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;
[currentWindow addSubview:myView];

更新—适用于Swift 4.1及以上

let currentWindow: UIWindow? = UIApplication.shared.keyWindow
currentWindow?.addSubview(myView)

iOS13及以上更新

已弃用keyWindow。你应该使用以下方法:

UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.addSubview(myView)

我建议你创建一个新的UIWindow:

UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = viewController;
window.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
window.opaque = NO;
window.windowLevel = UIWindowLevelCFShareCircle;
window.backgroundColor = [UIColor clearColor];

[window makeKeyAndVisible];

然后你可以在另一个UIViewController中管理你的视图。 拆除窗户:

[window removeFromSuperview];
window = nil;

希望对大家有所帮助!


将您的视图添加为NavigationController的子视图。

[self.navigationController.navigationBar addSubview: overlayView)]

你也可以把它加在窗户上:

UIView *view = /* Your custom view */;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window addSubview:view];

希望这能有所帮助。:)


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


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

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

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

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

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


Dalef伟大的解决方案在swift:

self.navigationController?.view.addSubview(view)

这里有一个简单的优雅的解决方案,是为我工作。你可以将导航栏的z位置设置为视图的下方:

self.navigationController.navigationBar.layer.zPosition = -1;

只要记住在完成时将其设置为0即可。


我将使用一个UIViewController子类,包含一个“容器视图”,它嵌入了你的其他视图控制器。 然后,您将能够在容器视图中添加导航控制器(例如使用嵌入关系segue)。

参见实现容器视图控制器


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

Swift版本的检查响应:

Swift 4:

let view = UIView()
view.frame = UIApplication.shared.keyWindow!.frame
UIApplication.shared.keyWindow!.addSubview(view)

Swift 3.1:

let view = UIView()
view.frame = UIApplication.sharedApplication().keyWindow!.frame 
UIApplication.sharedApplication().keyWindow!.addSubview(view)

@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
}

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


[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];


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

添加这些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()
}

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

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


@Nam的答案是伟大的,如果你只是想显示你的自定义视图,但如果你的自定义视图需要用户交互,你需要禁用导航栏的交互。

self.navigationController.navigationBar.layer.zPosition = -1
self.navigationController.navigationBar.isUserInteractionEnabled = false

就像Nam的回答中所说的,不要忘记逆转这些变化:

self.navigationController.navigationBar.layer.zPosition = 0
self.navigationController.navigationBar.isUserInteractionEnabled = true

你可以用一个扩展更好的方式做到这一点:

extension UINavigationBar {
    func toggle() {
        if self.layer.zPosition == -1 {
            self.layer.zPosition = 0
            self.isUserInteractionEnabled = true
        } else {
            self.layer.zPosition = -1
            self.isUserInteractionEnabled = false
        }
    }
}

就像这样简单地使用它:

self.navigationController.navigationBar.toggle()

在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中

这是你的视图

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

You need to add a subview to the first window with the UITextEffectsWindow type. To the first, because custom keyboards add their UITextEffectsWindow, and if you add a subview to it this won't work correctly. Also, you cannot add a subview to the last window because the keyboard, for example, is also a window and you can`t present from the keyboard window. So the best solution I found is to add subview (or even push view controller) to the first window with UITextEffectsWindow type, this window covers accessory view, navbar - everything.

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)