Hassy
Hassy

Reputation: 5208

Add multiple UIWindow

I am adding a new UIWIndow over another to display a view, but it is not showing anything and the screen just gets a little blurred. Here is the code:

UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[topWindow setWindowLevel:UIWindowLevelNormal];

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;

UIViewController* viewController = [[UIViewController alloc] init];
UIView* overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight,   viewController.view.frame.size.width, statusBarHeight - 1)];
[overlay setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[overlay setBackgroundColor:[UIColor whiteColor]];
[viewController.view addSubview:overlay];
[topWindow setRootViewController:viewController];

[topWindow setHidden:NO];
[topWindow setUserInteractionEnabled:NO];
[topWindow makeKeyAndVisible];

viewController = nil;

overlay = nil;

What am I doing wrong?

Upvotes: 3

Views: 9593

Answers (3)

Tony
Tony

Reputation: 4591

I wanna do like you too.

@implementation TAAppDelegate {
    UIWindow *win;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        win = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        win.rootViewController = [UIViewController new];
        win.rootViewController.view.backgroundColor = [UIColor whiteColor];
        win.rootViewController.view.alpha = 0.5;
        [win makeKeyAndVisible];
    });
    return YES;
}
.....

And I did it. I think you should retain your new UIWindows. (I'm using ARC so I defined 1 local var to retain it)

Good luck!

Upvotes: 4

ader
ader

Reputation: 5393

Why are you trying to create a second window to overlay the main one?

From the Apple docs:

"A UIWindow object coordinates the presentation of one or more views on a screen. Most apps have only one window, which presents content on the main screen, but apps may have an additional window for content displayed on an external display."

You should be presenting your additional viewController modally or using "UIViewController containment". All within the one window.

Upvotes: 1

vietstone
vietstone

Reputation: 9092

Set windowLevel property to another value. I usually use:

topWindow.windowLevel = UIWindowLevelAlert + 1;

Upvotes: 2

Related Questions