如何将我的应用限制为横向模式?

时间:2010-11-02 14:02:01

标签: iphone cocoa-touch ipad orientation landscape

我使用SplitView模板创建了我的iPad应用程序。 我想知道将我的应用程序限制为横向模式的最佳方法是什么?

我尝试在DetailViewController.m中重写shouldAutorotateToInterfaceOrientation:方法

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

4.2 GM仍然有错误 ,无法显示控制器视图。我还有其他选择吗?

提前致谢。

UPDATE1

  • 我已经提交了错误报告: Bug ID #8620135

  • 我的应用程序差不多完成了,我必须找到一个工作环节,因为我认为他们不会在4.2正式发布之前解决这个问题(GM已经出局了!)

    为了重新创建bug,只需在任何UIViewControllers(RootViewController或DetailViewControllers)中使用SplitView模板并覆盖上面的方法

UPDATE2

我找到了解决办法。 (有关完整的解决方法,请参阅UPDATE3)

仅将UISupportedInterfaceOrientations设置为支持横向,这将强制应用程序以横向模式启动,从而允许DetailViewController正确启动(因此显示正确)

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

但是如果你旋转设备,它会变成肖像模式!!!,所以仍然需要覆盖 shouldAutorotateToIntercafeOrientation:如上所述

讨论:

如果这不是一个错误,我会期望在视图控制器不支持的方向启动应用程序时出现警告或执行错误,异常或其他问题。此外,为什么只有DetailViewController不显示?如果这是规范,那么RootViewController也应该无法加载。你不觉得吗? 谢谢你的帮助...;)

UPDATE3

经过进一步测试后,我意识到上述解决办法在某些情况下不起作用。例如,当设备处于横向状态时启动应用程序将无法正常工作! 真正的问题似乎是在iOS4.2GM中,UISplitViewController需要其所有控制器在其加载时具有所有旋转。因此有必要欺骗他,以便在横向模式下加载,然后不允许他旋转其视图控制器。

所以这是烦人的iBug的新解决方案。

步骤1: 像这样设置Info.plist:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

第二步 在DetailViewController.m或.h(来自SplitView模板)中设置新标志

BOOL lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    //WORK-ARROUND: Bug ID# 8620135.
    if (lockRotation) {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);
    }else{
        return YES;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //set NO here since this is called before shouldAutorotateToInterfaceOrientation method is called
    lockRotation = NO; //WORK-ARROUND: Bug ID# 8620135.
}
- (void)viewDidAppear:(BOOL)animated {
    //set YES as soon as possible, but after shouldAutorotateToInterfaceOrientation method was called
    lockRotation = YES; //WORK-ARROUND: Bug ID# 8620135.
    [super viewDidAppear:animated];
}

重要提示:  请注意,此错误仅在加载UISplitViewController时出现,而不是每次都出现  它的视图出现了。因此,要查看此错误,请确保该应用程序之前已终止。

4 个答案:

答案 0 :(得分:2)

我问了500个seems to be the same thing you're facing的赏金问题。

从我有限的经验来看,制作仅限风景的iPhone应用程序比仅使用风景的iPad应用程序要容易得多。我不确定为什么会有任何差别,但Apple表示采取的步骤只能让它成为一个独立的步骤。

答案 1 :(得分:1)

试试这个(它有效):

-(BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)toInterfaceOrientation {
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
        return YES;
    }
    else 
    {
        return NO;
    }
}

答案 2 :(得分:0)

如果您还没有,请查看此iPhone app in landscape mode。我打算建议简单地将UISupportedInterfaceOrientations添加到Info.plist并指定两个横向方向。但是,根据引用问题的答案,显然这还不够。

答案 3 :(得分:0)

我相信这是一个BUG,我也遇到了这个问题。这与

有关
UIInterfaceOrientationLandscapeLeft

要复制这种情况:

1)使用UISplitViewController模板

创建一个新的iPad项目

2)编辑info.plist

Supported interface orientations
-Landscape (left home button)
-Landscape (right home button)

3)DetailViewController.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
//    return YES;
 NSLog(@"RotateToInterface:[%d] vs LandscapeLeft[%d]", interfaceOrientation, UIInterfaceOrientationLandscapeLeft);
 return interfaceOrientation == UIInterfaceOrientationLandscapeLeft;
}

4)运行它....你会看到一个空白的黑色视图。无论你如何转身。从未检测到“UIInterfaceOrientationLandscapeLeft”。

顺便说一下, nacho4d 添加BOOL检查解决方法正在运行。竖起大拇指:)

相关问题