【问题标题】:Supporting different orientations in iOS 6.0 for different view controllers在 iOS 6.0 中支持不同视图控制器的不同方向
【发布时间】:2012-10-07 08:58:24
【问题描述】:

我的应用中有一个自定义拆分视图控制器,带有一个主控制器和一个详细控制器。

- (id)initWithMasterController:(UIViewController*)aMasterController
            detailedController:(UIViewController*)aDetailedController;

主控制器和细节控制器提供的控制器是UINavigationController。

作为我的应用程序的一部分,方向处理有两种可能的情况:

  1. 当主控制器和细节控制器使用六种控制器组合时,应用程序支持所有方向。
  2. 当仅在详细信息控制器上存在 StudentDetailsViewController 时,只能支持两种可能的方向。 (风景)

当设备的方向发生变化时,iOS 6.0 以下的版本会发生以下情况

  1. -shouldAutorotateToInterfaceOrientation: 方法被调用。该方法的实现如下:在运行时,我通过相同的调用将请求转发到主控制器和细节控制器。

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {   
        BOOL res = [masterController shouldAutorotateToInterfaceOrientation:interfaceOrientation]
                   && [detailedController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        return res;
    }
    
  2. 主控制器的-shouldAutorotateToInterfaceOrientation 将返回TRUE。 StudentViewController 中方法的实现如下。

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

获取有关要更改的新方向的信息的能力有助于我决定是否应启用旋转。

使用 iOS 6.0:

当设备的方向改变时,iOS 6.0 版本会发生以下情况

  1. 分割视图控制器的-shouldAutorotate 方法被调用。它的实现如下

    - (BOOL)shouldAutorotate {
        BOOL res = [masterController shouldAutorotate]
                   && [detailedController shouldAutorotate];
        return res;
     }
    
  2. detailedController 的 shouldAutorotate 调用 navigationController。 StudentsController 中自动旋转功能的实现:

    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        return (UIInterfaceOrientationMaskLandscapeLeft
                | UIInterfaceOrientationMaskLandscapeRight);
    }
    

但在 iOS 6.0 中,我无法控制方向。即使调用了supportedInterfaceOrientations方法,但是当调用StudentDetailsController的shouldAutorotate方法时,从detailsController的shouldAutorotate方法中,shouldAutorotateMethod不服从supportedInterfaceOrientations方法中提到的选项。

更新:

我阅读了文档,document 中提供了以下注释。

有时您可能希望动态禁用自动旋转。为了 例如,当您想抑制旋转时,您可能会这样做 完全在短时间内。您必须暂时禁用 方向改变你想手动控制的位置 状态栏(例如当您调用 setStatusBarOrientation:animated: 方法)。

如果您想暂时禁用自动旋转,请避免 操纵方向蒙版来做到这一点。相反,覆盖 最顶层视图控制器上的 shouldAutorotate 方法。这种方法是 在执行任何自转之前调用。如果返回 NO,则 旋转被抑制。

是否可以暂时禁用基于当前方向的自动旋转?

【问题讨论】:

  • 无法正确格式化代码..
  • 问题解决了吗?我有同样的问题,不知道如何为 ios5 和 6 处理这个问题
  • 不,我还没有解决它..

标签: objective-c ios6 uiinterfaceorientation


【解决方案1】:

我相信这是 iOS 中的某种类型的问题,其中 rootViewController 不会咨询 childViewController 的首选方向。但是,您应该尝试以下方法:

 if (self.interfaceOrientation != UIInterfaceOrientationPortrait) 
{
    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
}

将给定视图的方向更改回纵向。

【讨论】:

    【解决方案2】:

    在您的应用程序委托类中定义以下方法,此方法在应用程序中的任何其他旋转方法之前被调用。

    创建一个标志(isRotationEnabled),它将决定您的应用程序的方向。

    - (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    
    
    return self.isRotationEnabled ?
        UIInterfaceOrientationMaskAll :
        UIInterfaceOrientationMaskPortrait;
    }
    

    使用以下代码根据您应用中的不同条件更改此标志

    MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.isRotationEnabled = NO;
    

    【讨论】:

      猜你喜欢
      • 2014-08-27
      • 2023-03-03
      • 2012-09-28
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多