简体   繁体   中英

IOS: How to put some view on top of presented modal view controller?

I have an activity view that I have added in AppDelegate class to tap bar:

[self.mainTabBar.view addSubview: spinner];

When there are connection problems it is visible in each view controller and is spinning. There is some button at certain view controller, makes to present some modal view controller. That modal view controller overlaps the spinner. How to make that spinner always be on top of all views or at least on top of that modal view controller? I tried to make such a thing in view controller that presents modal view controller:

[self presentModalViewController:selectionViewController animated:YES];
[self.view bringSubviewToFront:[self.tabBarController.view viewWithTag:15]];

Not works.

Add the view to the main window.

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview: spinner];

While phix23's answer is correct, here is a more complete example:

//The view you want to present
UIViewController *viewControllerYouWantToPresentOnTop = [[UIViewController alloc] initWithNibName:nil bundle:nil];

//Create transparent host view for presenting the above view
UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *viewControllerForPresentation = [[UIViewController alloc] init];
[[viewControllerForPresentation view] setBackgroundColor:[UIColor clearColor]];
[[viewControllerForPresentation view] setOpaque:FALSE];
[mainWindow addSubview:[viewControllerForPresentation view]];

//Make your transparent view controller present your actual view controller
[viewControllerForPresentation presentViewController:viewControllerYouWantToPresentOnTop animated:TRUE];

Remember to clean up after yourself when you don't need these any longer.

This code can be used from anywhere in your app, even a library :)

An app normally displays its content within a single window throughout its life. But there are situations where an extra window may be used to add content on top of everything else. Apple ensures UIAlertView always stays on top by adding it in a separate window.

UIView *contentView = [[UIView alloc] initWithFrame:contentFrame];
contentView.backgroundColor = [UIColor greenColor];
UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(x,y,contentFrame.size.width, contentFrame.size.height)];
window.windowLevel = UIWindowLevelAlert;
[window addSubview:contentView];
[window makeKeyAndVisible];

Show and hide your window by setting window.hidden = Yes or No as needed. This will always show your contentView on top of everything else in the app.

Place the view to the keyWindow, as suggested above. You might also need to set Presentation style of the modal view as Current Context, otherwise, it can still pop on top

The modal controller is in a completely different layer, you cannot make any subview of the presenting controller to overlap it.

Use a UIAlertView with a spinner inside. The alerts are displayed in a layer which overlaps even modal controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM