6

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

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

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

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

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

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

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

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {   
        BOOL res = [masterController shouldAutorotateToInterfaceOrientation:interfaceOrientation]
                   && [detailedController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        return res;
    }
    
  2. masterController-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. detailController 的 shouldAutorotate 调用了 navigationController。StudentsController 中自动旋转功能的实现:

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

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

更新:

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

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

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

是否可以根据当前方向暂时禁用自动旋转?

4

2 回答 2

0

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

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

- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {    

return self.isRotationEnabled ?
    UIInterfaceOrientationMaskAll :
    UIInterfaceOrientationMaskPortrait;
}

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

MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.isRotationEnabled = NO;
于 2013-05-10T17:10:43.613 回答
0

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

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

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

于 2012-12-31T01:43:13.727 回答