【发布时间】: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