Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a UIView above all, even the navigation bar

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 alpha to see the other UIViewController underneath.
  • a UIView window in the middle with some information, (a calendar if you want to know everything).

To do that, I've created a UIViewController that contains the two UIViews (background and window), and I'm trying to display it. I've tried a simple [mySuperVC addSubview:myPopUpVC.view], but I still have the navigation bar above.

I've tried to present it as a modal, but the UIViewController underneath disappears, and I lose my transparency effect.

Any idea to do this, I'm sure it's quite simple...

Thanks!

like image 437
Nicolas Roy Avatar asked Feb 18 '14 10:02

Nicolas Roy


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.

What is a navigation controller Swift?

A navigation controller is a container view controller that manages one or more child view controllers in a navigation interface. In this type of interface, only one child view controller is visible at a time.


2 Answers

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

like image 39
dalef Avatar answered Oct 30 '22 19:10

dalef


You can do that by adding your view directly to the keyWindow:

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

UPDATE -- For Swift 4.1 and above

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

UPDATE for iOS13 and above

keyWindow is deprecated. You should use the following:

UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.addSubview(myView) 
like image 121
Rom. Avatar answered Oct 30 '22 18:10

Rom.