UINavigationController + shouldAutoRotate + no sub

2019-08-12 18:25发布

问题:

I have a navigation driven app. I need that app to rotate. The UINavigationController is the root controller/view in the window. I know (and have experienced why) it is a no-no to subclass UINavigationController. I know all i have to do is insert:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

into the UINavigationController and it will rotate fine.

So my question is: how do I enable rotation on the root view controller (UINavigationController) WITHOUT subclassing it?

回答1:

You need to override this method in your rootViewController, not in UINavigationController.

UIViewController *rootViewController = [[MyRootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[rootViewController release];


回答2:

Your UINavigationController inherits from UIViewController - why would using the method you show be a bad thing? It is perfectly legitimate to use a super's method and is the only way I have ever supported rotation in a UINavigationController. Wouldn't subclassing be when you inherit from UINavigationController (and override that method to do something else without calling the super method?)