【问题标题】:How to restrict my app to landscape mode?如何将我的应用程序限制为横向模式?
【发布时间】:2011-05-03 23:13:16
【问题描述】:

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

我已经尝试在 DetailViewController.m 中覆盖 shouldAutorotateToInterfaceOrientation: 方法

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

4.2 GM 仍然存在错误,并且无法显示控制器视图。我还有什么其他选择?

提前致谢。

更新1

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

  • 我的应用程序快完成了,我必须找到一个解决方法,因为我认为他们不会在 4.2 正式发布之前解决这个问题(GM 已经发布了!)

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

更新2

我找到了解决方法。 (完整的解决方法请参见 UPDATE3)

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

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

但是如果你旋转设备,它会变成纵向模式!!!,所以还是需要覆盖shouldAutorotateToIntercafeOrientation:同上

讨论:

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

UPDATE3

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

所以这里是解决这个烦人的 iBug 的新方法。

第一步: 像这样设置 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 时出现,而不是每次都出现 它的视图出现。因此,要查看此错误,请确保应用程序之前已终止。

【问题讨论】:

  • 问题表明您正在 DetailViewController 中进行更改,是覆盖该方法的正确控制器吗?
  • @Aaron 在我进行更改的控制器中并不重要。 (在RootViewController或DetailViewController中)结果是一样的。
  • 基本上是 UISupportedInterfaceOrientations,什么都不做。要在应用启动时设置方向,请在 plist 中设置 UIInterfaceOrientation。 (它的友好名称是 Initial Interface Orientation)

标签: iphone cocoa-touch ipad orientation landscape


【解决方案1】:

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

根据我有限的经验,制作仅横向的 iPhone 应用程序要比仅横向的 iPad 应用程序容易得多。我不知道为什么会有任何不同,但 Apple 所说的使其成为横向的步骤并不能单独工作。

【讨论】:

  • 我想我在那个线程中找到了一个提示,这就是我所做的并且它有效,但这肯定是一个解决方法。我相信这不应该是必要的。查看我的更新2。
  • 即使艰难,我也找到了自己的答案……这个答案让我找到了答案。所以谢谢;)
【解决方案2】:

试试这个(它有效):

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

【讨论】:

  • 根据你想要的模式修改'if'条件。上面的代码将其限制为横向。将“if”检查更改为 UIInterfaceOrientationLandscapeLeft 以锁定左侧。对于两种横向模式,请执行 if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
  • 没用...我相信不使用 UISplitViewController 就足够了。
  • UIInterfaceOrientationIsLandscape(interfaceOrientation); 是 Apple 宏,用于执行您在回答中所说的内容。所以提问者已经按照你说的做了。
  • 我的意思是,UIInterfaceOrientationIsLandscape(interfaceOrientation); 与您的答案相同。
  • 不幸的是,它对提问者不起作用,但就像我说它对我有用,我在 iPad 的应用商店上有一个应用,它使用仅横向的 splitviewcontroller。我用过这种方式,它对我有用。哦,好吧......只要你找到了解决方案,一切都很好
【解决方案3】:

我相信这是一个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 检查的解决方法正在运行。竖起大拇指:)

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多