0

I have a view controller in my app. Let its first view controller, my first view controller appears in portrait mode in phone, when user rotate phone in landscape mode, first view controller also rotate in landscape mode.

Its working fine, and now I have a button on first view controller, when I touch the button second view controller appears. I just want to do is that the second view controller should always appear in portrait mode, even though the first view controller is in landscape mode. Is there any methods which I have to override to get this functionality?

MouseCrasher
  • 453
  • 1
  • 5
  • 14

2 Answers2

1

In a navigation controller, the orientation of your controller depend on the orientation of the navigation controller´s root controller.

You have two possibilities:

  1. make your root controller's shouldAutorotateToInterfaceOrientation: return different values depending on which controller is actually shown;

  2. use a transform on you your view controller's view so that it is rotated.

I would give a try to the first way, to start. Have a look at this post for an idea how to do it (just ignore the UITabBarController stuff), or try this (which simply relays the message to the top controller in your navigation hierarchy):

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {   
    return [self.navigationController.topController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
 }

In order to achieve the same result on iOS6, try and define the following methods:

-(NSUInteger)supportedInterfaceOrientations {
  return [self.navigationController.topController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return [self.navigationController.topController  preferredInterfaceOrientationForPresentation];
}
Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
  • Sir I am using ios6. And I created a controller subclass of UINavigationController. Above method is deprecated in ios6. So I used shouldAutorotate method for it. And then I used the my first view controller as root view controller of the navigation controller. But its not working sir. – MouseCrasher Jan 08 '13 at 10:05
  • Though deprecated, you still need `shouldAutorotateToInterfaceOrientation` if you want your app to run on iOS5 -- I simply thought you would apply the same reasoning to the iOS6 methods (which I have not used in this context yet). In any case, check my edit for an iOS6 version of the solution. I hope it works, but as I said, I have not tried it. – sergio Jan 08 '13 at 10:17
1
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

In second view controller keep this.

Rajkumar
  • 179
  • 6