允许一个视图支持多个方向,而其他人不支持iPhone

时间:2011-04-20 18:41:42

标签: iphone objective-c cocoa-touch ios orientation

我的情况是我通过UITabBarController控制了多个视图。其中一个视图是通过UINavigationController控制的。应用程序应仅支持所有视图的肖像...除了一个单独的视图。此视图被推入导航控制器的堆栈,并应允许纵向和横向。

我已经尝试了所有我能想到的解决方案但是,要么解决方案不起作用,要么更糟糕的是完全无法预测。

有人以干净的方式解决了这个问题吗?

4 个答案:

答案 0 :(得分:3)

您使用presentModalViewControllerpushViewController呈现的视图控制器应使用此方法支持任何方向:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

使用此方法,当您以横向显示控制器时,它也会以横向方向正确显示。

答案 1 :(得分:1)

我遇到了同样的问题。您必须为shouldAutorotateToInterfaceOrientation:实施UITabBarController,并为所有视图控制器支持的所有方向返回YES。并且在所有其他视图控制器的相同方法中,仅针对您希望在每个视图控制器中支持的方向返回YES。

要为您的UITabBarController实现shouldAutorotateToInterfaceOrientation:,您可以继承UITabBarController,但更简单的方法是在UITabBarController上实现一个类别并实现该方法:

@implementation UITabBarController(orientation)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation
{
    return YES; // or whatever you need
}

@end

注意:输入网络浏览器;没编译。 : - )

您可以将此权限放在最后的app delegate .m文件中。

我发现UITabBarController仅为肖像返回YES,这显然也会影响所有从属视图控制器。在我的情况下,我有一个从属视图控制器,我想支持纵向和横向,这解决了它。

答案 2 :(得分:1)

如果您在标签栏中有导航,那么唯一的选择是将特定视图显示为模态视图(标签栏需要所有视图以支持自动旋转的方向)。所以基本上需要景观的单视图控制器应该实现

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
          // Create the landscape version of the view and present it modally
      }

      return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

然后通过检测纵向方向并使用父[self.parentViewController dismissModalViewControllerAnimated: animated]

的方法,在模态视图控制器的同一方法中关闭模态视图

答案 3 :(得分:0)

对于iOS-6,我已经完成了这项工作,它运行得很好

(NSUInteger)supportedInterfaceOrientations
{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{ 
    return UIInterfaceOrientationLandscapeLeft;
}

这会对你有帮助....

相关问题