【问题标题】:navigate the hierarchy of modal viewcontroller and navigationcontroller导航模态视图控制器和导航控制器的层次结构
【发布时间】:2015-02-06 10:05:04
【问题描述】:

我正在做this,试图在横向中只看到一个画廊,但是当我以模态方式将照片库与主 ViewController 连接时,我只能显示横向:

RootViewController -(modal segue)> PhotoGallery

问题出在我这样做的时候:

Rootviewcontroler -(modal segue)> ViewControllerModal -(modal segue)> PhotoGallery

它不起作用,也不适用于导航控制器:

RootViewController -(modal segue)> NavigationController A -(push segue)> NavigationController B -(modal segue)> PhotoGallery

我也不知道如何在supportedInterfaceOrientationsForWindow 中将层次结构向上导航到图库:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

if ([self.window.rootViewController.presentedViewController isKindOfClass: [PhotoGallery class]]){
    PhotoGallery *photoGallery = (PhotoGallery *) self.window.rootViewController.presentedViewController;
    if (photoGallery.isPresented) return UIInterfaceOrientationMaskLandscape; 
    else return UIInterfaceOrientationMaskPortrait;
}

return UIInterfaceOrientationMaskPortrait;

}

提前致谢。

【问题讨论】:

    标签: ios objective-c iphone orientation hierarchy


    【解决方案1】:

    我曾经为一个电子商务应用程序这样做过,并且必须呈现一个签名屏幕是横向的,而该应用程序的其余部分是纵向的。为了实现这一点,我们首先在 UIViewController 上创建了一个类别来支持强制方向

    @implementation UIViewController (ForcedOrientation)
    
    -(UIInterfaceOrientationMask)forcedOrientation {
        // Default implementation is to return none (i.e. no forced orientations);
        return UIInterfaceOrientationMaskPortrait;
    }
    
    -(void) forceOrientationAdjustment {
        UIViewController *root = [[[UIApplication sharedApplication] keyWindow] rootViewController];
        UIViewController *dummy = [[UIViewController alloc] init];
        [root presentViewController:dummy animated:NO completion:^{
            [root dismissViewControllerAnimated:NO completion:nil];
        }];
    }
    
    @end
    

    然后我们继承 UINavigationController 并覆盖以下方法:

    - (BOOL)shouldAutorotate
    {
        return YES;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        UIViewController *visibleVC = self.topViewController;
    
        // Need to find out what the actual "top screen" is
        while (visibleVC) {
            if ([visibleVC isKindOfClass:[UINavigationController class]]) {
                visibleVC = [(UINavigationController*)visibleVC topViewController];
                continue;
            }
            break;
        }
    
        NSUInteger forcedOrientation = [visibleVC forcedOrientation];
        if (forcedOrientation) {
            return forcedOrientation;
        } else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    最后一步是在我们希望在横向中呈现的视图控制器中添加一个强制方向调用,并在我们的 viewDidAppear 方法中添加对 forceOrientationAdjustment 的调用:

    - (UIInterfaceOrientationMask) forcedOrientation {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    -(void) viewDidAppear:(BOOL)animated {
    ...
        [self forceOrientationAdjustment]
    ...
    }
    

    虽然这很有效,但对我来说总是感觉像是一个 hack。为了改变方向,我们必须非常快速地展示这个假视图控制器。可能有更好的解决方案,也许是这个:How to force a UIViewController to Portrait orientation in iOS 6

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2014-10-10
      相关资源
      最近更新 更多