【问题标题】:iPhone - allow landscape orientation on just one viewcontrolleriPhone - 仅在一个视图控制器上允许横向
【发布时间】:2010-01-27 04:12:53
【问题描述】:

我有一个基于导航的应用程序,我希望其中一个视图控制器支持横向。对于该视图控制器(vc1),在 shouldAutorotate 中,我为所有方向返回 YES,而在其他控制器中,我仅在纵向模式下返回 YES

但即使设备处于横向模式并且我从 vc1 转到下一个屏幕,下一个屏幕也会以横向模式旋转。我假设如果我只为纵向模式返回 YES,则屏幕应该只以纵向显示。

这是预期的行为吗?我如何实现我的目标?

谢谢。

【问题讨论】:

标签: iphone cocoa-touch


【解决方案1】:

如果您使用 UIViewController 的 shouldAutorotateToInterfaceOrientation 方法,则不能仅支持其中一个视图控制器的横向。
您只有两个选择,是所有视图控制器都支持横向还是没有视图控制器支持它。

如果您只想支持一个横向,您需要检测设备旋转并在视图控制器中手动旋转视图。
您可以使用 Notification 检测设备旋转。

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

然后,您可以在检测到设备旋转时旋转视图。

- (void)didRotate:(NSNotification *)notification {
    UIDeviceOrientation orientation = [[notification object] orientation];

    if (orientation == UIDeviceOrientationLandscapeLeft) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI / 2.0)];
    } else if (orientation == UIDeviceOrientationLandscapeRight) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI / -2.0)];
    } else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
        [xxxView setTransform:CGAffineTransformMakeRotation(M_PI)];
    } else if (orientation == UIDeviceOrientationPortrait) {
        [xxxView setTransform:CGAffineTransformMakeRotation(0.0)];
    }
}

【讨论】:

  • 这正是我需要的,你能告诉我如何旋转 UIToolBar 和 UINavigationBar
  • 这就像一个魅力。但是状态栏还有问题吗?如何随设备方向旋转它?
【解决方案2】:

当我需要所有视图控制器处于纵向模式时,我也遇到过这种情况,但其中一个也可以旋转到横向模式。这个视图控制器有导航栏。

为此我创建了第二个窗口,在我的例子中它是相机视图控制器。当我需要显示相机视图控制器时,我会显示相机窗口并在我需要推送另一个视图控制器时隐藏。

您还需要将此代码添加到 AppDelegate。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{   
    if (window == self.cameraWindow)
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    return UIInterfaceOrientationMaskPortrait;
}

【讨论】:

    【解决方案3】:

    当我为我的应用程序设计时,我建议您使用此解决方案。通过在 shouldAutorotateToInterfaceOrientation 方法方向类型中使用一些条件,我们可以解决此问题。尝试使用此链接将对您有所帮助。

    https://stackoverflow.com/questions/12021185/ios-rotate-view-only-one-view-controllers-view/15403129#154031

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-24
      • 2013-06-15
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多