9

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];
Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
  • set its `rootViewController` and `makeKeyAndVisible`; in your application (after did-finish-lauch!) if you are not using a `UIWindow` like this, that means clearly that you _do not_ need to use `UIWindow` for the actual task. – holex Mar 20 '15 at 08:38

8 Answers8

11

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

Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
iamVishal16
  • 1,780
  • 18
  • 40
7

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];
}
TienLe
  • 614
  • 2
  • 9
  • 33
  • huh? it will work. I use this code to custom(fake) popoverView or alertView. You can add this code bellow: [window bringSubviewToFront:aView]; – TienLe Mar 20 '15 at 06:58
  • 1
    My storyboard: http://i.cubeupload.com/fnceNh.png aView will not show if you do not set rootviewcontroller – TienLe Mar 20 '15 at 07:21
  • When I'm adding this view it adds successfully!! But My Navigation Bar slightly hides behind this view. Any Suggestion?? @TienLe – Parth Barot Jul 25 '19 at 09:53
6

Try this code:

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

Replace the following code:

window = [[UIApplication sharedApplication].windows objectAtIndex:0];
cakan
  • 2,099
  • 5
  • 32
  • 42
Steven Gardner
  • 234
  • 3
  • 9
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];
Prajeet Shrestha
  • 7,978
  • 3
  • 34
  • 63
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • This seems to work but the new view is covering whole screen. It's frame is { (0,0) (320,568) } – Prajeet Shrestha Mar 20 '15 at 06:36
  • yes the viewcontroller.view covers the whole screen. aView which is the view you want has size 100, 100 – Nikos M. Mar 20 '15 at 06:38
  • What I wanted to do was make it appear like alertView. Transparent background. But it will overlap the viewController viewController from which this code is called. – Prajeet Shrestha Mar 20 '15 at 06:48
  • When I'm adding this view it adds successfully!! But My Navigation Bar slightly hides behind this view. Any Suggestion?? @NikosM. – Parth Barot Jul 25 '19 at 09:53
4

You can add view using the following

[[[UIApplication sharedApplication] keyWindow] addSubview:YOUR_VIEW];
Shekhu
  • 1,998
  • 5
  • 23
  • 49
2

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

Vidhyanand
  • 5,369
  • 4
  • 26
  • 59
  • When I'm adding this view it adds successfully!! But My Navigation Bar slightly hides behind this view. Any Suggestion?? @vidhyanand – Parth Barot Jul 25 '19 at 09:52
2

i think you are missing this. check once.

[self.window makeKeyAndVisible];
Mahesh Agrawal
  • 3,348
  • 20
  • 34
2

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.

Nitheesh George
  • 1,357
  • 10
  • 13