How to add view in UIWindow?

允我心安 提交于 2019-12-21 04:18:22

问题


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 this code:

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

Replace the following code:

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



回答3:


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];
}



回答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.



来源:https://stackoverflow.com/questions/29160540/how-to-add-view-in-uiwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!