iPhone - 仅在一个viewcontroller上允许横向显示

los*_*sit 10 iphone cocoa-touch

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

但即便如此,如果设备处于横向模式并且我从vc1进入下一个屏幕,则下一个屏幕也会以横向模式旋转.我假设如果我仅为肖像模式返回YES,则屏幕应仅以纵向显示.

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

谢谢.

tom*_*ute 24

如果使用UIViewController的shouldAutorotateToInterfaceOrientation方法,则不能仅为其中一个视图控制器支持横向.
您只有两个选择,即所有视图控制器是否支持格局或视图控制器是否支持它.

如果只想为一个支持景观,则需要检测设备旋转并在viewcontroller中手动旋转视图.
您可以使用通知检测设备旋转.

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

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

- (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)];
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

当我在portait模式下需要所有视图控制器时,我也有这种情况,但其中一个也可以旋转到横向模式.而这个视图控制器有导航栏.

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

而且您还需要将此代码添加到AppDelegate.

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

    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)