4

I have app that supports all kind of orientations, however i want certain viewControllers to support only landscape and few to lock on Potrait Mode. How can i do that, I have tried below code but nothing works.

- (BOOL)shouldAutorotate {
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
// pre-iOS 6 support
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
sergio
  • 68,819
  • 11
  • 102
  • 123
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

1 Answers1

3

All view controllers pushed on to the same navigation controller must support the same orientation. Specifically, I believe that only the root view controller shouldAutorotateToInterfaceOrientation is ever taken into account. You can have a look at this S.O. post for a sort of workaround; or at this one for a different approach that could be of help.

The second post I linked above suggests using this definition for shouldAutorotateToInterfaceOrientation you might tweak it for your case:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
  {   
  if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
    UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers objectAtIndex:0];
    return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
  }
  return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123