16

所以我正在开发一个 iPad 应用程序,它只支持横向模式,除了一个模态视图控制器。我遇到的问题是,一旦我呈现模态视图并将方向更改为纵向然后关闭视图,父视图(应该只支持横向)处于纵向模式,直到我旋转它然后返回的设备景观并保持这种状态。我一直在自责,试图弄清楚如何让父母看到原来的方向,但一直没能找到解决办法。

我的应用程序委托中有以下代码,仅允许在该单个模态视图 (GalleryPhotoViewer) 上更改方向:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];

        //Support Portrait mode only on Photoviewer
        if ([[presentedViewController presentedViewController] isKindOfClass:GalleryPhotoViewController.class] ) {
            orientations = UIInterfaceOrientationMaskAll;
        }else{
            orientations = [presentedViewController supportedInterfaceOrientations];

        }
    }

    return orientations;
}

从父类(PhotosViewController)我打电话:

GalleryPhotoViewController *gpView = [GalleryPhotoViewController new];
[self presentViewController:gpView animated:YES completion:nil];

同样在我的父母(和其他视图)中,我有以下代码来禁止纵向模式:

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationPortrait) {
       return YES;
    } else {
        return NO;
    }
}

关于如何保持父视图方向的任何想法?一旦模式被解除,我正在考虑可能只是在 viewWillAppear 方法中以编程方式更改父级的方向,但是我不知道以前的方向是什么,更不用说我无法找到代码来做到这一点不管ios6。

编辑/解决方案:所以我找到了一个解决方案,我最终做的是离开应用程序:supportedInterfaceOrientationsForWindow: 代码,只是将 UINavigation 子类添加到呈现模态视图的父视图中,一切都按预期工作,父级保留其原始方向,而模态可以自由改变。

在我的父母中:

//To make sure that this view remains in Landscape
@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

@end

感谢@matt 的建议。

4

1 回答 1

12

我认为问题在于您使用application:supportedInterfaceOrientationsForWindow:. 相反,摆脱它,并从 UINavigationController 子类开始,并将其作为您的导航界面的根视图控制器的类。然后:

  • 在 UINavigationController 子类中,UIInterfaceOrientationMaskLandscapesupportedInterfaceOrientations.

  • 在呈现的(模态)视图控制器中,UIInterfaceOrientationMaskAllsupportedInterfaceOrientations.

于 2013-05-02T02:55:21.083 回答