objective-c - 在 iOS 6.0 中支持不同 View Controller 的不同方向

标签 objective-c ios6 uiinterfaceorientation

我在我的应用程序中有一个自定义 Split View Controller ,带有一个主 Controller 和一个详细 Controller 。

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

为主 Controller 和细节 Controller 提供的 Controller 是UINavigationController。

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

  1. 当在主 Controller 和细节 Controller 中使用六种 Controller 组合时,应用程序支持所有方向。
  2. 当仅在详细信息 Controller 处有一个 StudentDetailsViewController 时,只能支持两种可能的方向。 (横向)

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

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

    - (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. Split View Controller 的方法 -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 方法,当从 detailsController 的 shouldAutorotate 方法调用 StudentsDetailsController 的 shouldAutorotate 方法时,shouldAutorotateMethod 不遵守 supportedInterfaceOrientations 方法中提到的选项。

更新:

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

sometimes you may want to dynamically disable automatic rotation. For example, you might do this when you want to suppress rotation completely for a short period of time. You must temporarily disable orientation changes you want to manually control the position of the status bar (such as when you call the setStatusBarOrientation:animated: method).

If you want to temporarily disable automatic rotation, avoid manipulating the orientation masks to do this. Instead, override the shouldAutorotate method on the topmost view controller. This method is called before performing any autorotation. If it returns NO, then the rotation is suppressed.

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

最佳答案

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

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

将给定 View 的方向改回纵向。

关于objective-c - 在 iOS 6.0 中支持不同 View Controller 的不同方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12928607/

相关文章:

ios - UITableView滚动滞后,即使是简单文本

使用对象的 Objective-C 开关?

ios - 重写不在 super 中的方法

ios - 在 UITabBarController 中强制横向显示特定的 View Controller

iphone - 当方向改变时如何管理应用程序?

objective-c - 无法运行首选项 Pane 应用程序

objective-c - 有 NSWindowRestoration 的例子吗?

ios - 为什么我的界面被压扁了?

ios6 - UINavigationBar titleView 中 iOS 6 和 UIActivityIndi​​cator 的问题

uiviewcontroller - 当我插入具有不同支持方向的 Controller 时,为什么 UINavigationController 不改变方向?