How to add view in UIWindow?

2020-06-16 02:01发布

问题:

I wanted to add a view in UIWindow with following code:

 AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
 UIWindow *window = delegate.window;
 UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
 aView.backgroundColor = [UIColor blackColor];
 [window addSubview:aView];

This code didn't work. I wanted to clone property of UIAlertView. It will pop over top of everything when we call [alertViewInstance show]; method.

Tried this as well:

   UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    UIWindow* window = [UIApplication sharedApplication].keyWindow;

    if (!window) {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }

    [window addSubview:aView];
    [window bringSubviewToFront:aView];

回答1:

If are you using UINavigationController Use:

[self.navigationController.view.window addSubview:aView];

If are you using UITabBarController Use:

[self.tabBarController.view.window addSubview:aView];

In AppDelegate you can directly assign a view to window. In appDelegate didFinishLaunchingWithOptions method Use:

[self.window addSubview:aView];

Hope it helps...



回答2:

Try with this code:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    if (!window) {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }

    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    aView.backgroundColor = [UIColor redColor];
    aView.center = window.center;
    [window insertSubview:aView aboveSubview:self.view];
    [window bringSubviewToFront:aView];
}


回答3:

Try this code:

window = [[UIApplication sharedApplication].windows lastObject];

Replace the following code:

window = [[UIApplication sharedApplication].windows objectAtIndex:0];


回答4:

Your windows needs to have a root view controller.

AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

UIWindow *window = delegate.window;
UIViewController *controller = [[UIViewController alloc] init]; 
window.rootViewController = controller;
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
aView.backgroundColor = [UIColor blackColor];
[controller.view addSubview:aView];


回答5:

You can add view using the following

[[[UIApplication sharedApplication] keyWindow] addSubview:YOUR_VIEW];


回答6:

You can try below code...for adding subview to window

UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
aView.backgroundColor = [UIColor blackColor];
[[UIApplication sharedApplication].keyWindow addSubview:aView];

//Removing

[aView removeFromSuperview];

Hope it helps you..!



回答7:

i think you are missing this. check once.

[self.window makeKeyAndVisible];


回答8:

UIAlertView creates another UIWindow and set this window as key window when you call show method. So if you want to show your own alert view create a UIWindow instance show it and add your custom alert view to this newly created window.