【问题标题】:Auto-Resizing a dynamic view when rotated旋转时自动调整动态视图的大小
【发布时间】:2011-09-13 16:02:37
【问题描述】:

我有一个标签栏应用程序,一切正常。我可以使用各种 Tab Bar View 控制器旋转设备。

唉,有人建议几个视图控制器需要一个帮助页面。为此,我创建了一个新的 ViewController,其中包含一个 UIWebView(可以将帮助内置到 HTML 文件中)。

我创建新的“HelpViewController”如下:

mpHelpPage     = [[HelpPageViewController alloc] init];
[mTabBarController.view addSubview: mpHelpPage.view];
[mpHelpPage retain];

mpHelpPage.view.alpha = 0.75f;

当我处于肖像模式时,这会毫无问题地打开帮助页面。不幸的是,当我处于横向模式并执行上述代码时,它会在纵向中添加 HelpViewController(这意味着它会延伸到屏幕底部)。

因此,当我分配上面的 ViewController 时,我需要某种方式告诉 ViewController 旋转到当前设备方向。

然而,我不知道如何让它做到这一点。任何帮助将不胜感激!

【问题讨论】:

    标签: iphone objective-c uiviewcontroller autorotate


    【解决方案1】:

    我通过在viewWillAppear: 中进行方向检查来处理这个烦恼,例如

    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || 
        self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        // custom code or call willRotate
    } else {
        // custom code or call willRotate
    }    
    

    如果您愿意,也可以这样做

    if (UIDeviceOrientationIsPortrait(self.interfaceOrientation)) {
        // custom code or call willRotate
    } else {
        // custom code or call willRotate
    }    
    

    【讨论】:

    • 您将如何进行方向检查,我是否必须手动移动所有内容?我可以只调整视图控制器的超级视图的大小并期望所有子视图也调整大小吗?
    • 只要每个子视图都有一个自动调整大小的掩码,您就可以调整超级视图的大小。我更喜欢调用我的 willRotateToInterfaceOrientation:duration: 函数以避免冗余代码。
    • 明白。但是我怎么知道设备的方向呢?
    • [[UIDevice currentDevice] orientation];
    • 我应该只能调用“[self willRotateToInterfaceOrientation: [[UIDevice currentDevice]orientation] duration:1.0f];”吗?因为这似乎不起作用:(
    【解决方案2】:

    您应该在 ViewController 的 willRotateToInterfaceOrientation:duration: 中设置子视图的 frame-property

    或者您编写自己的视图并在视图的layoutSubviews 中设置框架属性

    添加的子视图应该处理其子视图的布局。

    【讨论】:

      【解决方案3】:

      由于您已将HelpViewController 添加为子视图并且没有UIViewController 控制它,因此不会调整其大小。您可以通过检测当前UIViewControllershouldAutorotateToInterfaceOrientation: 方法中的方向变化来手动调整HelpViewController 的视图大小。此方法将当前方向作为其参数传递,因此只需检查哪个是当前方向并将框架相应地设置为:

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
      {
          if((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight))
              mpHelpPage.view.frame = CGRectMake(0,0,480,300);
          else
              mpHelpPage.view.frame = CGRectMake(0,0,320,460);
          return YES;
      }
      

      或者,不要将HelpViewController添加为子视图,而是尝试[self.navigationController pushViewController:HelpViewController animated:YES];

      【讨论】:

      • 然后你应该把它推到视图控制器的堆栈上。现在,您仍然只是将其添加为子视图!
      • 但是我没有使用导航控制器……我使用的是标签栏控制器……如果我在标签栏控制器的导航控制器上使用 PushViewController,那么不出所料,什么也不会发生。
      • 同意,这只有在您已经拥有particular tab 的导航控制器时才有效...这就是为什么您必须手动调整它的大小。
      猜你喜欢
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      相关资源
      最近更新 更多