UINavigationController 中的 ViewController 方向更改 [英] ViewController in UINavigationController orientation change

查看:28
本文介绍了UINavigationController 中的 ViewController 方向更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下层次结构:

So I have the following hierarchy:

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

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

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

I want to lock the orientation on RootViewController to Portrait only, but leave all orientations for the rest view controllers.

如果我把它放到子类 UINavigationController:

If I put this to subclassed UINavigationController:

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

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

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

All view controllers are then locked to portrait.

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

My question is, is there a way to lock only RootViewController to Portrait, but leave all options for other view controllers?

推荐答案

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

check the link here for fixing autorotation in iOS 6 and set orientation support per view basis: http://www.disalvotech.com/blog/app-development/iphone/ios-6-rotation-solution/

你可以这样做:

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

 - (BOOL)shouldAutorotate
 {
      return self.topViewController.shouldAutorotate;
 }
 - (NSUInteger)supportedInterfaceOrientations
 {
      return self.topViewController.supportedInterfaceOrientations;
 }

  • 在你的 AppDelegate.h,

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

    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;
    
      }
    

  • 在您的 rootViewController 中,无论事件推送第二个视图控制器,请执行以下操作:

  • in your rootViewController, for whatever the event push the second view controller, do this:

      - (IBAction)pushSecondViewController:(id)sender {
    
        // push second view controller
        SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
        [self.navigationController pushViewController:secondViewController animated:YES];
       }
    

  • 在每个视图控制器中,添加

  • in your each view controller, add

       - (BOOL)shouldAutorotate{}
       - (NSUInteger)supportedInterfaceOrientations{}
    

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

    for iOS 6, you can set each view controller whatever the orientation support you want individually.

    对于iOS 5及以下,可以设置

    for iOS 5 and below, you can set

       - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{}
    

  • 所有功劳归于在链接中编写示例应用的 John DiSalvo.

    All the credits goes to John DiSalvo who wrote the sample app in the link.

    希望这会有所帮助.

    这篇关于UINavigationController 中的 ViewController 方向更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆