Search code examples
iosrotationios8uiwindow

UIWindow in iOS8 doesn't rotate correctly


I need to load storyboard programmatically, in iOS7 the following code works:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // need to comment out in iOS8
    self.window.backgroundColor = [UIColor cyanColor];
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.window.rootViewController = [storyboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];
    return YES;
}

But in iOS8 (XCode 6.1), the above code doesn't auto rotate correctly:

wrong rotation and wrong rotation

I need to comment out self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; to correct the auto rotation.

Any idea that first line makes what different?


Solution

  • Well, I find the answer from developer forum. Just remove the UIMainStoryboardFile key from app's information property list.

    "Your application's information property list contains the UIMainStoryboardFile key but you are also manually creating a UIWindow with a storyboard loaded manually in your app delegate.

    When the UIMainStoryboardFile key is present in the information property list, the system creates a UIWindow object with the initial view controller from the storyboard corresponding to the value of the UIMainStoryboardFile key prior to calling your app delegate. Your app delegate then repeats this process, determining which storyboard to load based on the screen size, and creating another UIWindow with the initial view controller from the storyboard. The window created by the system, being the first window, receives orientation change notification first. Due to a change in iOS 8.1, the first window ends up blocking the second window (the one you created, the key window) from responding to the orientation change. From what I have discerned, this only happens if the first windows rootViewController's view is not loaded (the original window is never made visible). I'm clarifying the details with engineering and will log any needed bug reports myself.

    The solution for your app is to remove the UIMainStoryboardFile and UIMainStoryboardFile~ipad keys from your app's information property list."