Trick preferredInterfaceOrientationForPresentation在viewController上改变 [英] Trick preferredInterfaceOrientationForPresentation to fire on viewController change

查看:189
本文介绍了Trick preferredInterfaceOrientationForPresentation在viewController上改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MSNavigationPaneViewController 从这里,有旋转的工作。
我已经覆盖了我的根中的旋转方法 UINavigationController

I am using MSNavigationPaneViewController from here and have rotation sort of working. I've overridden the rotation methods in my root 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 使用 presentViewController:animated:completion:和我有旋转工作,所以某些视图可以有不同的方向。问题是,具有不同取向的每个视图需要用户倾斜手机以改变在正确方向锁定的点的方向。
我已经做了大量的阅读,以获得这个工作,看起来我需要 preferredInterfaceOrientationForPresentation 在每个视图加载之前发射,但它不发射。
我认为它不会被触发,因为 MSNavigationPaneViewController 不会改变视图控制器使用 presentViewController
这是 MSNavigationPaneViewController 用来更改视图的代码

which I push the MSNavigationPaneViewController with using presentViewController: animated:completion: and I have rotation working so certain views can have different orientations. The problem is, each view that has a different orientation needs the user to tilt the phone to change the orientation at which point it locks on the correct orientation. I've done tons of reading to get this working and it seems I need preferredInterfaceOrientationForPresentation to fire before each view is loaded, but it's not firing. I think it's not firing because the MSNavigationPaneViewController isn't changing view controllers using presentViewController. This is the code used by MSNavigationPaneViewController to change the view

- (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

which looks like it's just switching the viewController and therefor not calling preferredInterfaceOrientationForPresentation

有任何方式强制 preferredInterfaceOrientationForPresentation

感谢

编辑 - ---

我完全理解新的旋转系统在iOS6中是如何工作的,它是由root来管理的。但是 preferredInterfaceOrientationForPresentation 只有在下一个视图显示时才会被调用。不是如果窗口刚刚切换。
所以我实际上需要一个方法来欺骗这个被调用。

I totally understand how the new rotation system works in iOS6 and that it's down to the root to manage it. However preferredInterfaceOrientationForPresentation only seems to be called if the next view is being presented with one of the correct methods. Not if the window is just switched. So I do actually need a way to trick this to be called.

推荐答案

通过重置其rootViewController来评估其支持的接口方向。

You can trick the window into re-valuating its supported interface orientations by resetting its rootViewController.

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

您需要确保在执行此代码时,root视图控制器的-supportedInterfaceOrientations会返回适当的掩码(对应于你想强制的方向)。

You'd need to make sure that when this code executes, -supportedInterfaceOrientations of the root view controller returns the proper mask (corresponding to how you want to force the orientation).

请注意,此黑客攻击可能会导致屏幕上快速闪烁,并会影响正在进行的动画。

Be aware, this hack can cause a quick flash on the screen and glitch any ongoing animations.

这篇关于Trick preferredInterfaceOrientationForPresentation在viewController上改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆