支持iOS 6.0中针对不同视图控制器的不同方向

Kri*_*nan 6 objective-c uiinterfaceorientation ios6

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

- (id)initWithMasterController:(UIViewController*)aMasterController
            detailedController:(UIViewController*)aDetailedController;
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {   
        BOOL res = [masterController shouldAutorotateToInterfaceOrientation:interfaceOrientation]
                   && [detailedController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
        return res;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. masterController -shouldAutorotateToInterfaceOrientation将返回TRUE.StudentViewController中方法的实现如下.

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (IS_IPAD) ? UIInterfaceOrientationIsLandscape(interfaceOrientation)
                         : UIInterfaceOrientationIsPortrait(interfaceOrientation);
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

使用iOS 6.0:

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

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

    - (BOOL)shouldAutorotate {
        BOOL res = [masterController shouldAutorotate]
                   && [detailedController shouldAutorotate];
        return res;
     }
    
    Run Code Online (Sandbox Code Playgroud)
  2. detailedController的shouldAutorotate调用navigationController.在StudentsController中实现autorotate功能:

    - (BOOL)shouldAutorotate {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations {
        return (UIInterfaceOrientationMaskLandscapeLeft
                | UIInterfaceOrientationMaskLandscapeRight);
    }
    
    Run Code Online (Sandbox Code Playgroud)

但是对于iOS 6.0,我无法控制方向.即使supportedInterfaceOrientations方法被调用,当StudentsDetailsController的shouldAutorotate方法被调用,从detailsController的shouldAutorotatemethod,该shouldAutorotateMethod不服从在supportedInterfaceOrientations方法中所提到的选项.

更新:

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

有时您可能想要动态禁用自动旋转.例如,如果要在短时间内完全抑制旋转,可以执行此操作.您必须暂时禁用要手动控制状态栏位置的方向更改(例如,当您调用setStatusBarOrientation:animated:方法时).

如果要暂时禁用自动旋转,请避免操作方向掩码来执行此操作.而是覆盖最顶层视图控制器上的shouldAutorotate方法.在执行任何自动旋转之前调用此方法.如果返回NO,则抑制旋转.

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

Aru*_*Das 0

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

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

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