Add a UIView above all, even the navigation bar

前端 未结 17 1633
悲&欢浪女
悲&欢浪女 2020-11-29 15:36

I want to display, above any other views, even the navigation bar, a kind of \"pop-up\" view that looks like this:

  • full screen black background with a 0.5 alph
相关标签:
17条回答
  • 2020-11-29 15:52

    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)
    
    0 讨论(0)
  • 2020-11-29 15:54

    [self.navigationController.view addSubview:overlayView]; is what you really want

    0 讨论(0)
  • 2020-11-29 15:54

    Swift versions for the checked response :

    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)
    
    0 讨论(0)
  • 2020-11-29 15:56

    Add you view as the subview of NavigationController.

    [self.navigationController.navigationBar addSubview: overlayView)]
    

    You can also add it over the window:

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

    Hope this helps.. :)

    0 讨论(0)
  • 2020-11-29 15:57

    I'd use a UIViewController subclass containing a "Container View" that embeds your others view controllers. You'll then be able to add the navigation controller inside the Container View (using the embed relationship segue for example).

    See Implementing a Container View Controller

    0 讨论(0)
  • 2020-11-29 15:58

    Here is a simple elegant solution that is working for me. You can set the z position of the navigation bar to below the view:

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

    Just remember to set it back to 0 when done.

    0 讨论(0)
提交回复
热议问题