title: iOS 6: How do I restrict some views to portrait and allow others to rotate?
body: <p>I have an iPhone app that uses a <code>UINavigationController</code> to present a drill-down interface: First one view, then another, up to four levels deep. I want the first three views restricted to portrait orientation and only the last view should be allowed to rotate to landscape. When returning from the fourth view to the third and the fourth view was in landscape orientation I want everything to rotate back to portrait.</p>
  
  <p>In iOS 5 I simply defined <code>shouldAutorotateToInterfaceOrientation:</code> in each of my view controllers to return YES for the allowable orientations. Everything worked as described above, including the return to portrait even if the device was being held in landscape orientation when returning from view controller #4 to #3.</p>
  
  <p>In iOS 6 all view controllers rotate to landscape, breaking those that weren't meant to. The iOS 6 release notes say </p>
  
  <blockquote>
    <p>More responsibility is moving to the app and the app delegate. Now, iOS containers (such as <code>UINavigationController</code>) do not consult their children to determine whether they should autorotate. [...] The system asks the top-most full-screen view controller (typically the root view controller) for its supported interface orientations whenever the device rotates or whenever a view controller is presented with the full-screen modal presentation style. Moreover, the supported orientations are retrieved only if this view controller returns YES from its <code>shouldAutorotate</code> method. [...] The system determines whether an orientation is supported by intersecting the value returned by the app’s <code>supportedInterfaceOrientationsForWindow:</code> method with the value returned by the <code>supportedInterfaceOrientations</code> method of the top-most full-screen controller.</p>
  </blockquote>
  
  <p>So I subclassed <code>UINavigationController</code>, gave my <code>MainNavigationController</code> a boolean property <code>landscapeOK</code> and used this to return the allowable orientations in <code>supportedInterfaceOrientations</code>. Then in each of my view controllers' <code>viewWillAppear:</code> methods I have a line like this</p>
  
  <pre><code>    [(MainNavigationController*)[self navigationController] setLandscapeOK:YES];
  </code></pre>
  
  <p>to tell my <code>MainNavigationController</code> the desired behavior.</p>
  
  <p><strong>Here comes the question:</strong> If I now navigate to my fourth view in portrait mode and turn the phone over it rotates to landscape. Now I press the back button to return to my third view which is supposed to work portrait only. But it doesn't rotate back. How do I make it do that?</p>
  
  <p>I tried</p>
  
  <pre><code>    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]
  </code></pre>
  
  <p>in the <code>viewWillAppear</code> method of my third view controller, but it doesn't do anything. Is this the wrong method to call or maybe the wrong place to call it or should I be implementing the whole thing in a totally different way?</p>