Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add view in UIWindow?

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];
like image 471
Prajeet Shrestha Avatar asked Mar 20 '15 06:03

Prajeet Shrestha


1 Answers

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

like image 143
iamVishal16 Avatar answered Sep 19 '22 14:09

iamVishal16