Search code examples
ioscocoa-touchuiwindow

Why are there two UIWindows in my app and why is the first one hidden?


Well, actually 3, but one is the UITextEffectsWindow.

So I started a simple test app.

AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    ViewController *controller = [[ViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
}

In the viewController I'm tagging the view:

    self.view.tag = 999;

When I log out this

    NSLog(@"[UIApplication sharedApplication].windows = %@",[UIApplication sharedApplication].windows);
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        NSLog(@"tag = %d",window.rootViewController.view.tag);
    }

I get this:

2015-04-04 08:59:44.440 SDK[45888:1495552] [UIApplication sharedApplication].windows = (
    "<UIWindow: 0x7fc57371e1e0; frame = (0 0; 375 667); hidden = YES; gestureRecognizers = <NSArray: 0x7fc57371eb10>; layer = <UIWindowLayer: 0x7fc57371c2f0>>",
    "<UIWindow: 0x7fc57352b0d0; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x7fc5750115f0>; layer = <UIWindowLayer: 0x7fc573526f20>>",
    "<UITextEffectsWindow: 0x7fc5750260c0; frame = (0 0; 375 667); opaque = NO; gestureRecognizers = <NSArray: 0x7fc575026fb0>; layer = <UIWindowLayer: 0x7fc575026580>>"
)
2015-04-04 09:03:33.319 SDK[46643:1499077] tag = 999
2015-04-04 09:03:33.319 SDK[46643:1499077] tag = 0
2015-04-04 09:08:11.648 SDKVevo[47379:1501367] key
2015-04-04 09:03:33.319 SDK[46643:1499077] tag = 0

As you can see, there are two UIWindows, and the first one is hidden. Moreover, the tag of the first one is 999, so that's the VC I instantiated and set as root. But how come that window is marked as hidden? And why are there two in the first place? And why is the second window logging out as the keyWindow?

When I run the app I actually see the VC I instantiated and tagged as 999, but the logs are saying it's part of the non-keyWindow, which is also a hidden window.


Solution

  • The problem is that you are creating a window:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        ...
    }
    

    ... but you forgot to stop the storyboard from also creating a window! If you are going to create a window manually like this, you should delete the storyboard and set the Info.plist so that it has no main storyboard setting. You cannot both create a window manually and use the storyboard - you've created an inner conflict in the structure of the app.