iPhone(iOS 5)强迫人像从风景和回归

时间:2013-04-02 11:28:23

标签: iphone objective-c cocoa-touch ios5 landscape-portrait

我正在研究iOS 5的应用程序,我有一个需要在纵向视图中推送的控制器。从景观中将控制器推向纵向模式是相当棘手的,但最后我想出了如何做到这一点。我按presentModalViewControllerpushViewController显示控制器,并在控制器内部覆盖preferredInterfaceOrientationForPresentation metod。

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

它工作得很好只是它有一个问题。以下是情景。

  1. 用户将设备保持在横向模式,当前控制器以横向模式正确显示
  2. 用户进行一些操作,仅显示纵向控制器(作为模态控制器和纵向模式)
  3. 用户关闭模态控制器
  4. 以前在风景中工作的控制器现在是纵向的,但是用户仍然将他的设备保持在风景中 - >这是问题
  5. 我尝试通过调用[UIViewController attemptRotationToDeviceOrientation]来修复它,但它没有帮助。

    感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:1)

我希望你能得到一个更好的,有效的解决方案我做了一件令人讨厌的工作。我的问题是我需要支持从4.3到6.0。一些pushModal函数已经彻底改变,被弃用等等,旋转也有变化。

我写了一个RoatatingViewController,我已添加到root。我根据需要添加了push modals和pushviewconcollers。 当我需要弹出时,我会检查:如果堆栈上的所需组件处于所需的方向而不是好的方向,则无需更改。否则:弹出横向并推送portait实例并将横向数据复制到portait。

这不是很好,它不是内存效率高,而是在不崩溃所有iOS版本的情况下工作。祝你好运!

答案 1 :(得分:1)

您说使用IOS 5 ,但preferredInterfaceOrientationForPresentation()方法仅从IOS 6 开始。

要解决您的问题(在ios5中),您需要覆盖shouldAutorotateToInterfaceOrientation()方法,如下所示......

在目标视图控制器(在纵向模式下需要查看控制器),就像这样....

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

<强>更新:

在当前视图控制器中(在横向模式下需要查看控制器),就像这样....

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