如何将我的应用限制为横向模式? [英] How to restrict my app to landscape mode?

查看:108
本文介绍了如何将我的应用限制为横向模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have my iPad application created using the SplitView template. I wonder what is the best way to restrict my application to landscape mode?

我试过覆盖 shouldAutorotateToInterfaceOrientation:方法

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

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

but 4.2 GM is still buggy and it fails to show the controller view. What other choices do I have?

提前致谢。

UPDATE1

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

My app is almost finished and I have to find a work-arround since I don't think they are going to solve this before 4.2 officially comes out (GM is already out!)

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

In order to recreate the bug, just use SplitView template and override above method in any of the UIViewControllers (RootViewController or DetailViewControllers)

UPDATE2

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

I have found a work-around. (See UPDATE3 for the complete work-around)

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

Set UISupportedInterfaceOrientations only to support Landscape , this will force the app to start in landscape mode allowing DetailViewController to start correctly(hence shown correctly)

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

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

But if you rotate the device, it turns Portrait mode!!!, so is still necessary to override shouldAutorotateToIntercafeOrientation: as above

讨论:

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

If this wouldn't be a bug I would expect a warning or execution error, exception or something when starting the app in a orientation that is not supported by the view controller. Besides, why only DetailViewController does not show? If this would be specification, then RootViewController should also fail to load then. Don't you think? thanks for you help... ;)

UPDATE3

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

After further tests I have realized that above work-around does not work in some cases. For example when starting the app when the device is in landscape won't work!. The real problem seems to be that in iOS4.2GM UISplitViewController needs all its controllers have all rotations to be available at its load time. So is necessary to trick him so it loads in Landscape mode and then not allow him to rotate its view controllers.

所以这是这个讨厌的iBug的新解决办法。

So here is the new work-around for this annoying iBug.

Step1:
像这样设置Info.plist:

Step1: Set Info.plist like so:

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

Step2
在DetailViewController.m或.h中设置一个新标志(来自SplitView模板)

Step2 Set a new flag in DetailViewController.m or .h (from SplitView Template)

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时出现,而不是每次出现
时出现。因此,要查看此错误,请确保该应用程序之前已终止。

IMPORTANT NOTE: Please note that this bug only appears when the UISplitViewController is loaded and not everytime the its view appears. Hence, to see this bug make sure the app was terminated before.

推荐答案

我问了一个500的赏金问题< a href =https://stackoverflow.com/questions/3972001/height-and-width-on-iphone-ipad>似乎与您面临的情况相同。

I asked a question with a bounty of 500 that seems to be the same thing you're facing.

从我有限的经验来看,制作仅限风景的iPhone应用程序比仅使用风景的iPad应用程序要容易得多。我不确定为什么会有任何差别,但Apple表示只采取横向设置的步骤并不能自行完成。

From my limited experience it is much easier to make a landscape-only iPhone app than a landscape-only iPad app. I'm not sure why there is any difference, but the steps Apple says to take to make it landscape-only do not work on their own.

这篇关于如何将我的应用限制为横向模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆