title: How to handle different orientations in iOS
body: <p>ADDED:
  You can access this project on github
  <a href="https://github.com/assistman/RotationIOS6" rel="nofollow noreferrer">ios6rotations</a></p>
  
  <hr>
  
  <p>Sorry guys for asking the question about screen rotation in iOS 6 but this is really a pain in the ass..and I still can't understand it completely - for some reason it behaves differently under certain circumstances.</p>
  
  <p>I have the following simple hierarchy of views in my test app:
  <img src="https://i.stack.imgur.com/0f6ms.png" alt=""></p>
  
  <p>What I'm trying to achieve is - <strong>to keep blue controller in landscape only and red one is only in portrait</strong>.</p>
  
  <p>I have a subclass of UINavigationController with such code inside:</p>
  
  <pre><code>@implementation CustomNavController
  
  - (BOOL)shouldAutorotate
  {
      return [[self.viewControllers lastObject] shouldAutorotate];
  }
  
  - (NSUInteger)supportedInterfaceOrientations
  {
      return  [[self.viewControllers lastObject] supportedInterfaceOrientations];
  }
  
  - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
      return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
  }
  
  @end
  </code></pre>
  
  <p>In my blue controller I implemented this:</p>
  
  <pre><code>- (BOOL)shouldAutorotate {
      return YES;
  }
  
  - (NSUInteger)supportedInterfaceOrientations {
      return UIInterfaceOrientationMaskLandscape;
  }
  </code></pre>
  
  <p>And in red controller this:</p>
  
  <pre><code>- (BOOL)shouldAutorotate {
      return YES;
  }
  
  - (NSUInteger)supportedInterfaceOrientations {
      return UIInterfaceOrientationMaskPortrait;
  }
  </code></pre>
  
  <p>Now I have the following behavior:</p>
  
  <ol>
  <li>App started in landscape (OK)</li>
  <li>When I press the button my red controller pushed in landscape too (this is not ok because it must be shown in Portrait)</li>
  <li>It successfully rotates to portrait but not backward to landscape</li>
  <li>If I leave the red controller in Portrait mode my blue controller (which is restricted to landscape) shows in Portrait mode.</li>
  </ol>
  
  <p>P.S.
  All my rotation methods(posted above) are getting called normally.(by the way why do these methods getting called so many times per screen transition - 5-6 times)</p>
  
  <p><strong><code>- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation</code> does not getting called with pushing</strong></p>
  
  <p>All(except portraitUpsideDown) orientations are included in plist.</p>
  
  <p><strong>The question is - how to force rotation to supported orientation in each controller?</strong></p>
  
  <p>I suggest you to post here (as answers) any 100% working code to handle rotations in ios6 (for example if you have some for iPad with SplitController) - I'll keep this question in favorites to have all in one place when I need to handle some specific situations. Cheers!</p>
  
  <p>ADDED:
  Please do not post this as answer <a href="https://stackoverflow.com/questions/14896607/ios6-0-pushing-new-viewcontroller-in-portrait-orientation-from-landscape-viewcon?rq=1">from landscape to portrait</a> I hope that there'
  s more elegant way to do it.</p>