How to add view in UIWindow?

前端 未结 8 1427
星月不相逢
星月不相逢 2021-02-14 22:42

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

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


        
相关标签:
8条回答
  • 2021-02-14 23:04

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

    0 讨论(0)
  • 2021-02-14 23:06

    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];
    
    0 讨论(0)
  • 2021-02-14 23:07

    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.

    0 讨论(0)
  • 2021-02-14 23:16

    You can add view using the following

    [[[UIApplication sharedApplication] keyWindow] addSubview:YOUR_VIEW];
    
    0 讨论(0)
  • 2021-02-14 23:18

    Try this code:

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

    Replace the following code:

    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    
    0 讨论(0)
  • 2021-02-14 23:20

    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..!

    0 讨论(0)
提交回复
热议问题