5

我知道你必须使用 IOS6 的新轮换方法,但我写的方法似乎不起作用。

我将我的 plist 文件设置为允许所有旋转,但不允许 PortraitUpsideDown

然后,我的appDelegate中有以下内容:

self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:navController];  //add nav controller to be the root view

然后在我的 rootView 中,要推送到另一个控制器,我有:

WebViewViewController *webController = [[JBWebViewViewController alloc] init];
webController.urlString =   urlName;
[self.navigationController pushViewController:webController animated:YES];

在网络控制器中,我有:

#pragma mark - System Rotation Methods
//for any version before 6.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//only allow landscape
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

/for 6.0+
- (BOOL)shouldAutorotate{
return NO;
}

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

我想要做的是在根视图中允许 3 次旋转,但是当切换到 Web 视图时(注意我是推送导航,而不是添加子视图),我只想允许纵向视图。

请有人帮助我

- - - -更新 - - - - -

我创建了自己的 UINavigationController 的 navController 子类,我有一个 BOOL LandscapeModeOn,我可以设置它来告诉自动旋转规范

#pragma mark - System Rotation Methods
//for any version before 6.0
- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation
{
  if (landscapeModeOn) {
    return interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
  } else {
    return interfaceOrientation == UIInterfaceOrientationPortrait;
  }
}

//for 6.0+
- (NSUInteger)supportedInterfaceOrientations{
  if (landscapeModeOn) {
    return UIInterfaceOrientationMaskAllButUpsideDown;
  } else {
    return UIInterfaceOrientationMaskPortrait;
  }
}

- (BOOL)shouldAutorotate{
  UIInterfaceOrientation ori = [UIDevice currentDevice].orientation;
  if (landscapeModeOn) {
    return ori != UIInterfaceOrientationPortraitUpsideDown;
  } else {
    return  ori == UIInterfaceOrientationPortrait;
  }
}

在子视图加载中,我这样做:

- (void)viewWillAppear:(BOOL)animated{
  //get nav controller and turn off landscape mode
  JBNavController *navController = (JBNavController*)self.navigationController;
  [navController setLandscapeModeOn:NO];
  [navController shouldAutorotate];
}

--------------------参考最佳答案引用 对于IOS6,苹果现在专注于使用Storyboard的AutoLayout以及新的旋转定义,难以修复一些基于 ios 4.3 和 ios 5 编码结构的 IOS6 小错误

从applefreak,他的建议暗示:

在您的情况下,一个主要挑战是不处理方向。实际上它将不同的视图控制器锁定到特定的方向

虽然手动旋转视图似乎真的很难没有任何错误,但它似乎是我现在正在尝试的唯一解决方案,一旦解决就会发布更多

4

3 回答 3

6

对于您的情况,您必须将 NavigationController 子类化并向其添加 shouldAutorotate 和 supportedInterfaceOrientations 方法。iOS6现在以与iOS5相反的顺序询问您的导航堆栈,因此它将首先询问您的NavigationController,如果返回YES,它甚至不会咨询它的子视图控制器。要解决这个问题,您必须自己添加逻辑来执行此操作

因此,在您的子类导航控制器中,您手动询问当前的视图控制器它的自动旋转能力:

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

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

并且在您的个人视图控制器中,您现在可以实现这些功能并让它们返回您在问题中定义的所需值。

我希望这是有道理的。

于 2012-09-24T21:47:30.037 回答
2

以下代码错误!

- (BOOL)shouldAutorotate{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

请记住,仅当 shouldAutoRotate 返回 YES 时才会调用supportedInterfaceOrientations。现在根视图控制器决定它的孩子是否旋转。

在您的情况下,我建议为您的 self.viewController 设置一个基类控制器并将 self.viewController 设置为根视图控制器而不是 navigationController 否则将不会调用旋转方法!我遇到了同样的问题。您应该与基本视图控制器及其子级具有 HAS-A 关系。根据活动的孩子从 ShouldAutoRotate 返回是/否,对于支持的方向也是如此。如果您遵循此架构,那么对于复杂的 App 将是一致的。

例如,在您的情况下,当 webviewController 处于活动状态时,BaseviewController 应该从 shouldAutoRotate 返回 YES 并从支持的方向委托返回 UIInterfaceOrientationPortrait。我希望这是有道理的。

于 2012-09-24T22:01:02.667 回答
0

iOS5和iOS6通用代码

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation))  
    { 
        // here to  implement landscope code
    }

    else
    {  
        // here to  implement setframePortrait
    }
}
于 2012-09-26T13:38:21.120 回答