2

In my mainwindow.xib I have a navigationcontroller. On top of (as a sub item) I have another viewcontroller(homeviewcontroller).

In the nib I have set the window's rootviewcontroller to be this navigationcontroller.

This is deployed to the app store and works perfectly.

Since upgrading to ios6 sdk I am getting orientation issues - basically with this design the supportedInterfaceOrientations method of my homeviewcontroller does not get called when running my app in ios 6 device/simulator.

In order to fix this issue I need to set homeviewcontroller as the window's rootviewcontroller however this is not what I want - I need the navigationcontroller.

How do I get around this annoying bug in ios6?

Update:

I have also tried doing this programmatically - it still doesn't work.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    HomeViewController *homeVC = [[HomeViewController alloc]init];
    UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:homeVC];
    [self.window setRootViewController:navController];

    [self.window makeKeyAndVisible];

    return YES; 
}
TheLearner
  • 19,387
  • 35
  • 95
  • 163

3 Answers3

7

You need to subclass UINavigationController and override supportedInterfaceOrientations there.

Felix
  • 35,354
  • 13
  • 96
  • 143
  • You're a legend thanks for your help. I am assuming this is a bug? – TheLearner Oct 15 '12 at 10:22
  • 2
    No this is not a bug. The behavior is explained here: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html *"When UIKit receives an orientation notification, it uses the UIApplication object and the **root view controller** to determine whether the new orientation is allowed. If both objects agree that the new orientation is supported, then the user interface is rotated to the new orientation. Otherwise the device orientation is ignored."* – Felix Oct 15 '12 at 12:19
  • 3
    Yes but should the navigation controller not pass this message to its child controllers? – TheLearner Oct 24 '12 at 10:56
  • The same documentation also mentions the following, so I'm with @TheLearner about being a bug: "_When a view controller is presented over the root view controller, the system behavior changes in two ways. First, the presented view controller is used instead of the root view controller when determining whether an orientation is supported_" – Oded Ben Dov Feb 19 '13 at 15:52
  • Personally I think this should fall on each view controller to be able to override this method as they are the View Controller. And by default, an app uses the orientations supported in the Target Summary – Adam Carter Mar 27 '13 at 13:39
1

To elaborate on @phix23's answer, I wrote this tiny UINavigationController subclass

@implementation MyNavigationController

- (NSUInteger) supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}

@end

From my understanding of the docs mentioned in his comment, this should be the default behavior. But somehow this works for me, while without it, it doesn't

Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
  • I found now this more thoroughly discussed here: http://stackoverflow.com/questions/12630359/ios-6-how-do-i-restrict-some-views-to-portrait-and-allow-others-to-rotate – Oded Ben Dov Feb 19 '13 at 16:10
0

Do it programmatically at runtime? (In your -applicationDidFinishLaunching: delegate method)

nielsbot
  • 15,922
  • 4
  • 48
  • 73