I have a UIViewController that returns YES in shouldAutorotateToInterfaceOrientation: for UIDeviceOrientationPortrait and NO
It has been a old post but since it hasn't been solved. I would like to share my solution, for any others who might be in a headache.
Objective: A UINavigationController and most of viewcontrollers in its stack fixed at portrait, except for one viewcontroller in the stack being allowed to rotate to both portrait and landscape.
Problem: intuitively I set an selective shouldAutorotateToInterfaceOrientation by checking if the topViewController is the rotableViewController. However after poping back from the rotableViewController at the landscape mode, the navigationcontroller is now shown in the landscape mode although it is not allowed.
Solution:The killer is to disallow the rotation at viewWillAppear and present & dismiss a modalViewController without animation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait) return YES;
return canRotate;
}
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewDidAppear:animated];
canRotate = ([navigationController.topViewController isKindOfClass:[MyRotatable class]]);
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[viewController viewWillAppear:animated];
if (![navigationController.topViewController isKindOfClass:[MyRotatable class]]) {
canRotate = NO;
UIViewController * blanck = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:blanck animated:NO];
[self dismissModalViewControllerAnimated:NO];
[blanck release];
}
}