代码之家  ›  专栏  ›  技术社区  ›  lostInTransit

iPhone-只允许在一个视图控制器上进行横向定位

  •  10
  • lostInTransit  · 技术社区  · 15 年前

    我有一个基于导航的应用程序,我只希望其中一个视图控制器支持横向。对于该视图控制器(VC1),在ShouldAutoRotate中,对于所有方向,我返回Yes;对于其他控制器,我返回Yes,仅对于Portrait模式。

    但即便如此,如果设备处于横向模式,我从VC1进入下一个屏幕,下一个屏幕也会在横向模式下旋转。我假设如果我返回Yes Only for Portrait模式,屏幕应该只以Portrait显示。

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

    谢谢。

    3 回复  |  直到 12 年前
        1
  •  24
  •   tomute    15 年前

    如果使用 应注意面间方向 uiviewController的方法。
    无论所有的ViewController支持横向还是不支持横向,您只有两个选择。

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

    [[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)];
        }
    }
    
        2
  •  5
  •   Salo Ievgen    12 年前

    我也有这样的情况,当我需要所有的视图控制器在门户模式,但其中一个也可以旋转到景观模式。这个视图控制器有导航栏。

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

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

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {   
        if (window == self.cameraWindow)
        {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    
        return UIInterfaceOrientationMaskPortrait;
    }
    
        3
  •  0
  •   Community CDub    8 年前

    当我为我的应用程序设计时,我建议你使用这个解决方案。通过在ShouldAutoToInterfaceOrientation方法方向类型中使用一些条件,我们可以解决这个问题。只需使用这个链接就可以了。

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