0

I want to create a loading view page to put on top of all the views in screen. I use the following code to get the rootViewController's view:

UIApplication.sharedApplication.keyWindow.rootViewController.view

I create my view page and add it as a subview to rootViewController's view. My problem is when I do this, if the rootViewController changes, my loading page will go behind the new viewController's view. For example, I am calling the showLoading function when I send an http request. When response comes back, it changes the viewController and loading goes behind it. Is there any way to put my loading on top independent from what happens in the background?

AliBZ
  • 4,039
  • 12
  • 45
  • 67

1 Answers1

3

You are allowed to create as many UIWindows as you'd like. If you really want to ensure a view will always be on top, then its best to create a second UIWindow and add it your the hierarchy. Then regardless of what happens in the other UIWindow your loading indicator will remain on top.

Example

UIWindow *secondWindow = [[UIWindow alloc] initWithFrame:[[[UIApplication sharedApplication] keyWindow] frame]];
[secondWindow setWindowLevel:UIWindowLevelStatusBar];
[secondWindow makeKeyAndVisible];

UIViewController *aNewViewController = [[UIViewController alloc] initWithFrame:secondWindow.bounds];
[secondWindow setRootViewController:aNewViewController];
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98