ios - 在以不同方向呈现模态视图后保持父 View 的方向

标签 ios ipad ios6 modalviewcontroller uiinterfaceorientation

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

我的应用程序委托(delegate)中有以下代码,仅允许在单个模态视图 (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];

同样在我的 parent (和其他观点)中,我有以下代码来禁止纵向模式:

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

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

关于如何保持父 View 方向的任何想法?我正在考虑可能只是在模态被关闭后以编程方式更改 viewWillAppear 方法中父级的方向但是我不知道以前的方向是什么,更不用说我无法找到代码来执行此操作不管 ios6.

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

在我的 parent 中:

//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 的建议。

最佳答案

我认为问题在于您对 application:supportedInterfaceOrientationsForWindow: 的使用。相反,摆脱它,从 UINavigationController 子类开始,并将其作为导航界面的 Root View Controller 的类。然后:

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

  • 在呈现的(模态) View Controller 中,从 supportedInterfaceOrientations 返回 UIInterfaceOrientationMaskAll

关于ios - 在以不同方向呈现模态视图后保持父 View 的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16329500/

相关文章:

iphone - 在 Xcode 中制作浏览器?

ios - UIDocumentInteractionController 不打开应用程序 (didEndSendingToApplication : never called)

iphone - iOS6以来按钮背景和渐变的变化

ios - 如何将事件指示器添加到 WKWebView (Swift 3)

ios - 用于在 mapView 上显示标记的 JSON 连接

iPhone/Ipad应用程序开发

android - 如何在移动浏览器中播放 swiffy HTML5 文件中的音频?

ios - 同时移动和更新 UITableViewCell 和 NSFetchedResultsController

ios - UIView 动画仅在呈现静态图像时触发

ios - 获取 Text 在 UITextview 中的位置