这是解决方案。如果您将 UIViewController 放到 window.root 中,并且您的导航控制器只是根控制器的一个属性,那么您的导航将不会从 UIApplication 获得适当的回调,因此您必须将导航作为子视图控制器添加到您的根控制器。
我的代码片段:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
MyNavigationController *navController = [[MyNavigationController alloc] initWithRootViewController:[ViewController1 new]];
MainViewController *main = [MainViewController new];
main.myNavigationController = navController;
[main addChildViewController:navController];
self.window.rootViewController = main;
[self.window makeKeyAndVisible];
return YES;
}
还需要覆盖 MainViewController 中的以下方法。
- (BOOL)shouldAutorotate
{
return [self.myNavigationController.topViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.myNavigationController.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.myNavigationController.topViewController preferredInterfaceOrientationForPresentation];
}