在模态视图之后保持父视图的方向呈现不同的方向

时间:2013-05-02 02:39:56

标签: ios ipad ios6 modalviewcontroller uiinterfaceorientation

所以我正在开发一个仅支持横向模式的iPad应用,除了在一个模态视图控制器上。我遇到的问题是,一旦我呈现模态视图并将方向更改为纵向然后关闭视图,父视图(应该只支持横向)处于纵向模式,直到我旋转设备然后返回到风景和保持这种方式。我一直在努力弄清楚如何让父母看到原始方向,但却找不到解决方案。

我的app appate中有以下代码,只允许在单个模态视图(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的建议。

1 个答案:

答案 0 :(得分:12)

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

  • 在UINavigationController子类中,从UIInterfaceOrientationMaskLandscape返回supportedInterfaceOrientations

  • 在呈现的(模态)视图控制器中,从UIInterfaceOrientationMaskAll返回supportedInterfaceOrientations

相关问题