Search code examples
iphoneobjective-csubviewuiinterfaceorientationuiwindow

UIInterfaceOrientation rotation in subview not working


In my App I use a UITabbarController, which rotates perfectly to all UIInterfaceOrientations in all viewcontrollers. But when I add an UIView to the UIWindow afterwards it will not be added in the current UIInterfaceOrientation, but always in UInterfaceOrientationPortrait (which is default for the app). It won't rotate to a new orientation also. I add the ViewController by using:

LoginViewController *loginViewController = [[LoginViewController alloc] init];
[self.window addSubview:[loginViewController view]];

I have

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSLog(@"%@", @"YES IT WILL!");
}

in LoginViewController.m but there will never be logged anything. Any idea why the subview won't rotate?

SideSwipe

EDIT: Found the solution: Apparently UIWindow should only have one subview, not more, otherwise things will mess up, so i call:

LoginViewController *loginViewController = [[LoginViewController alloc] init];
[tabBarController presentModalViewController:loginViewController animated:YES];

instead, which will autorotate the loginviewcontrollers view just fine.


Solution

  • I have a view inside another view, when rotation happened, only the parent willRotateToInterfaceOrientation got called, so what I did is add [self.subViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; to parent's willRotateToInterfaceOrientation method.