UINavigationController中的ViewController方向更改

133*_*ode 12 iphone orientation uinavigationcontroller ipad ios

所以我有以下层次结构:

UINavigationController- > RootViewController(UIViewController) - > UITableViewController- > DetailViewController(UIViewController)

我想将RootViewController上的方向锁定为仅限纵向,但保留其余视图控制器的所有方向.

如果我把它放到子类中UINavigationController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

然后将所有视图控制器锁定为纵向.

我的问题是,有没有办法只将RootViewController锁定为Portrait,但保留其他视图控制器的所有选项?

Ray*_*ang 27

检查此处的链接以在iOS 6中修复自动旋转并根据视图设置方向支持:http://www.disalvotech.com/blog/app-development/iphone/ios-6-rotation-solution/

这是你可以做的:

  1. UINavigationController在.m文件中创建一个自定义导航控制器,它是.的子类:

     - (BOOL)shouldAutorotate
     {
          return self.topViewController.shouldAutorotate;
     }
     - (NSUInteger)supportedInterfaceOrientations
     {
          return self.topViewController.supportedInterfaceOrientations;
     }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在你的AppDelegate.h,

      @interface AppDelegate : UIResponder <UIApplicationDelegate> {
    
          UINavigationController *navigationController;
          ViewController *viewController;
      }
    
      @property (strong, nonatomic) UIWindow *window;
      @property (strong, nonatomic) ViewController *viewController;
    
    Run Code Online (Sandbox Code Playgroud)

    AppDelegate.m,

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
            // set initial view
           self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
           viewController = [[ViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    
           navigationController = [[CustomNavigationController alloc]
                        initWithRootViewController:viewController]; // iOS 6 autorotation fix
           [navigationController setNavigationBarHidden:YES animated:NO];
    
            self.window = [[UIWindow alloc]
               initWithFrame:[[UIScreen mainScreen] bounds]];
    
            [self.window setRootViewController:navigationController]; // iOS 6 autorotation fix
            //[self.window addSubview:navigationController.view];
    
            [self.window makeKeyAndVisible];
    
    
             return YES;
      }
    
    
      - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
      {
              return UIInterfaceOrientationMaskAll;
    
      }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在你的rootViewController中,无论事件推送第二个视图控制器,执行以下操作:

      - (IBAction)pushSecondViewController:(id)sender {
    
        // push second view controller
        SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
        [self.navigationController pushViewController:secondViewController animated:YES];
       }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在每个视图控制器中,添加

       - (BOOL)shouldAutorotate{}
       - (NSUInteger)supportedInterfaceOrientations{}
    
    Run Code Online (Sandbox Code Playgroud)

    对于iOS 6,您可以设置每个视图控制器,无论您需要单独的方向支持.

    对于iOS 5及更低版本,您可以设置

       - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
    
    Run Code Online (Sandbox Code Playgroud)

所有学分都归John DiSalvo所有,他在链接中编写了示例应用程序.

希望这可以帮助.