2010-01-27 76 views
10

我有一个基于导航的应用程序,我只希望其中一个视图控制器支持横向。对于该视图控制器(vc1),在shouldAutorotate中,我将返回YES用于所有方向,而在其他控制器中,我只返回YES仅适用于肖像模式iPhone - 只允许一个视图控制器的横向方向

但即使如此,如果设备处于横向模式,我会转到下一个屏幕从vc1开始,下一个屏幕也会以横向模式旋转。我认为如果我只为肖像模式返回YES,屏幕只能以肖像模式显示。

这是预期的行为?我如何实现我所寻找的?

谢谢。

+0

可能重复:http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation – 2010-01-27 04:17:36

+0

我不想设置方向。我只想要其中一个视图不响应方向更改。 – lostInTransit 2010-01-27 04:27:32

+0

类似的问题也在http://stackoverflow.com/questions/2041884/problem-with-uiviewcontroller-orientation/2042040#2042040但没有答案添加。 – 2010-01-27 05:08:25

回答

24

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

如果您只想支持一种格局,您需要检测设备旋转并手动旋转视图控制器中的视图。
您可以使用通知检测设备旋转。

[[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)]; 
    } 
} 
+1

这就是我确切需要的可以告诉我如何旋转UIToolBar和UINavigationBar – Sam 2010-11-10 05:55:21

+0

非常有帮助! .. – Ahsan 2012-06-13 20:20:25

+1

这工作就像一个魅力。但状态栏仍然存在问题?如何使用设备方向旋转它? – 2013-02-06 10:30:04

5

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

为此,我创建了第二个窗口,在我的情况下是摄像头视图控制器。而当我需要显示相机视图控制器时,我会显示相机窗口,并在需要推送另一个视图控制器时隐藏。

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

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

    return UIInterfaceOrientationMaskPortrait; 
} 
相关问题