IOS: How to put some view on top of presented moda

2019-02-02 05:06发布

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.

4条回答
一夜七次
2楼-- · 2019-02-02 05:39

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.

查看更多
来,给爷笑一个
3楼-- · 2019-02-02 05:46

Add the view to the main window.

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
[mainWindow addSubview: spinner];
查看更多
放我归山
4楼-- · 2019-02-02 05:52

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

查看更多
Viruses.
5楼-- · 2019-02-02 05:53

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.

查看更多
登录 后发表回答