【问题标题】:Orientation Landscape -> Portrait isn't working方向横向 -> 纵向不起作用
【发布时间】:2011-11-07 11:16:58
【问题描述】:

我还有一个方向问题。但这一个非常棘手。 我的 RootViewController 是一个普通的 NavigationController。

self.window.rootViewController = _naviController;

里面有另一个ViewController,我们称之为VC1。 VC1 有一些按钮和标签。它就像一个文件夹概览。

如果我按下一个按钮,我会来到下一个 ViewController,其中包含 3 个 ViewController(页面)和另一组按钮(比如在文件夹中查看里面的图片/缩略图):

Archiv *archiv = [[Archiv alloc] init];
[self.navigationController pushViewController:archiv animated:YES];
[archiv release];

在加载视图中:

firstPage = [[Page alloc] initViewWithFrame:CGRectMake(0, 0, 768, 960)];
[firstPage setRootViewController:self];
secondPage = [[Page alloc] initViewWithFrame:CGRectMake(0, -960, 768, 960)];
[secondPage setRootViewController:self];
thirdPage = [[Page alloc] initViewWithFrame:CGRectMake(0, 960, 768, 960)];
[thirdPage setRootViewController:self];

如果我现在再次点击一个按钮,活动页面会推送我的第三个 ViewController(调整大小、拖动的图像...):

Picture *pic = [[Picture alloc] initWithPicURLString:urlString];
[rootViewController.navigationController pushViewController:pic animated:YES];
[pic release];

使用 NavigationController 的 BackButton,我总是可以返回到上一个视图。

更多信息:

  • 每个 ViewController 都支持所有方向
  • 每个 ViewController 都实现 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientationreturn YES;
  • 每个 ViewControler 在其 init 方法中调用 [super init]
  • 我已经看过Apple's Q&A: Why won't my UIViewController rotate with the device

现在棘手的问题:

如果我从第二个 VC 切换到第三个 VC,将那里的方向从 portrait 更改为 landscape 并按 BackButton 一切正常工作shouldAutorotateToInterfaceOrientation 正在调用,帧大小和原点变化...)。 但是如果我反其道而行之,我将处于 横向模式,从 2nd VC 切换到 3rd VC,旋转到纵向然后来使用 BackButton 回到第二个 VC,状态和控制器栏位于顶部,但 shouldAutorotateToInterfaceOrientation 没有被调用

请帮助我。 $h@rky

【问题讨论】:

    标签: ios xcode uinavigationcontroller orientation modalviewcontroller


    【解决方案1】:

    试试这个,它对我有用:

    -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self shouldAutorotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] ];
    }
    

    【讨论】:

    • 是的,这项工作也是如此,但这并不能回答为什么当我以纵向返回时视图没有调用的问题。无论如何,如果 nodoby 告诉我为什么下周会发生这种情况,我会接受答案。
    • 就像我承诺的那样接受和投票,因为分辨率比我的方式更聪明 xD。 $h@rky
    【解决方案2】:

    今天我得到了解决问题的想法,但不知道原因。 在我的第三个 VC 中,我刚刚创建了一个指向第二个视图的指针,并自己调用了 shouldAutorotateToInterfaceOrientation

    但重点还是一样:为什么shouldAutorotateToInterfaceOrientation 在所描述的情况下不打电话?

    亲切的问候。 $h@rky

    【讨论】:

      【解决方案3】:

      shouldAutorotateToInterfaceOrientation 仅在用户旋转时调用,所以当你从横向到纵向时,视图控制器仍然是横向的,所以这解决了问题,你必须破解代码,这意味着当你将视图控制器从横向推到纵向时presentViewController 示例:

      ListCurrentViewController *list = [self.storyboard
         instantiateViewControllerWithIdentifier:@"ListCurrentViewController"];
                     [self.navigationController presentViewController:list animated:NO completion:Nil];
                     [list dismissViewControllerAnimated:NO completion:Nil];
                     [self.navigationController pushViewController:list animated:YES];
      

      在 ListViewController 函数中调用:

      (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix { return UIInterfaceOrientationPortrait; }
      

      你必须为 UINavigationController 创建类别

      (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
          return [self.visibleViewController preferredInterfaceOrientationForPresentation];
      }
      

      我希望这个解决方案对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-30
        • 1970-01-01
        相关资源
        最近更新 更多