2

I have added a view to [[UIApplication sharedApplication] keyWindow], because we want it to overlap the navigation bar.

Now we want to tap a button on the view and open a window modally, ideally by using presentViewController. Is there a way to make this modal view appear above (on the z-axis) the keyWindow view?

My Answer

I ended up adding my first custom UIViewController to self.tabBarController. This allows the UIViewController to overlap the Navigation Bar and the Tab Bar.

Then I use [self.navigationController presentViewController:animated:completion] to present the modal, which is above everything else on the z-axis.

Here is the code for adding the UIViewController to the self.tabBarController

        MyCustomViewController * overlayView = [[MyCustomViewController alloc]
                      initWithNibName:@"MyCustom"
                      bundle:nil];

        UIViewController *parentViewController = self.tabBarController;
        [modalView willMoveToParentViewController:parentViewController];

        // set the frame for the overlayView
        overlayView.view.frame = parentViewController.view.frame;

        [parentViewController.view addSubview: overlayView.view];

        [parentViewController.view needsUpdateConstraints];
        [parentViewController.view layoutIfNeeded];

        // Finish adding the overlayView as a Child View Controller
        [parentViewController addChildViewController: overlayView];
        [overlayView didMoveToParentViewController:parentViewController];
Chris
  • 5,485
  • 15
  • 68
  • 130

1 Answers1

0

You should rethink the way you're adding the overlay view.

If you want a view to overlap the navigation bar you could just add this view as a top subview of the navigation bar. Or you could add it as a top subview of the navigation controller view.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • Sorry but can you expand on this a bit? I am not finding any info when I look for terms such as "navigation controller view" or "top subview". Pretty much everything I am finding about putting a `UIViewController` on top of the `Navigation Bar`, says to use keyWindow. http://stackoverflow.com/questions/21850436/add-a-uiview-above-all-even-the-navigation-bar – Chris Mar 08 '16 at 20:01