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

38,512

Solution 1

Add the view to the main window.

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

Solution 2

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 :)

Solution 3

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.

Share:
38,512
nik
Author by

nik

Updated on July 31, 2022

Comments

  • nik
    nik almost 2 years

    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.