7

MSNavigationPaneViewController 从这里使用并且有轮换工作。我已经覆盖了我的根目录中的旋转方法UINavigationController

-(BOOL)shouldAutorotate
{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.topViewController != nil) {
        return [appDelegate.topViewController supportedInterfaceOrientations];
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.topViewController != nil) {
        return [appDelegate.topViewController preferredInterfaceOrientationForPresentation];
    } else {
        return UIInterfaceOrientationPortrait;
    }
}

MSNavigationPaneViewController我用 using推动它,presentViewController: animated:completion:并且我有旋转工作,因此某些视图可以有不同的方向。问题是,每个具有不同方向的视图都需要用户倾斜手机来改变方向,在该点锁定正确的方向。我已经做了大量的阅读以使其正常工作,似乎我需要preferredInterfaceOrientationForPresentation在加载每个视图之前触发,但它没有触发。我认为它没有触发,因为MSNavigationPaneViewController没有使用presentViewController. 这是用于MSNavigationPaneViewController更改视图的代码

- (void)setPaneViewController:(UIViewController *)paneViewController
{
    if (_paneViewController == nil) {

        paneViewController.view.frame = _paneView.bounds;
        _paneViewController = paneViewController;
        [self addChildViewController:_paneViewController];
        [_paneView addSubview:_paneViewController.view];
        [_paneViewController didMoveToParentViewController:self];

    } else if (_paneViewController != paneViewController) {

        paneViewController.view.frame = _paneView.bounds;
        [_paneViewController willMoveToParentViewController:nil];
        [self addChildViewController:paneViewController];

        void(^transitionCompletion)(BOOL finished) = ^(BOOL finished) {
            [_paneViewController removeFromParentViewController];
            [paneViewController didMoveToParentViewController:self];
            _paneViewController = paneViewController;
        };

        [self transitionFromViewController:_paneViewController
                          toViewController:paneViewController
                                  duration:0
                                   options:UIViewAnimationOptionTransitionNone
                                animations:nil
                                completion:transitionCompletion];
    }
}

看起来它只是切换 viewController 并因此不调用preferredInterfaceOrientationForPresentation

有什么方法可以强制preferredInterfaceOrientationForPresentation被调用,或者通过隐藏视图诱骗它被调用?

谢谢

编辑 - -

我完全理解新的轮换系统在 iOS6 中是如何工作的,并且它是由根来管理的。但是,如果下一个视图使用正确的方法之一,preferredInterfaceOrientationForPresentation似乎只会被调用。presented如果窗口刚刚切换,则不会。所以我确实需要一种方法来欺骗这个被调用。

4

2 回答 2

9

您可以通过重置其 rootViewController 来欺骗窗口以重新评估其支持的界面方向。

UIApplication *app = [UIApplication sharedApplication];
UIWindow *window = [[app windows] objectAtIndex:0];
UIViewController *root = window.rootViewController;
window.rootViewController = nil;
window.rootViewController = root;

您需要确保在执行此代码时,根视图控制器的 -supportedInterfaceOrientations 返回正确的掩码(对应于您希望如何强制方向)。

请注意,此 hack 可能会导致屏幕快速闪烁并干扰任何正在进行的动画。

于 2012-12-21T21:21:10.627 回答
6

preferredInterfaceOrientationForPresentation不会在UINavigationController.

您将不得不子类化UINavigationController,然后自己实现此功能。您只想委托给topViewController.

供参考查看:http ://code.shabz.co/post/32051014482/ios-6-supportedorientations-with-uinavigationcontroller

我还发现了一个子类的这个实现UINavigationControllerhttps ://gist.github.com/3842178

于 2012-12-19T01:52:44.287 回答