导航模态viewcontroller和navigationcontroller的层次结构

时间:2015-02-06 10:05:05

标签: ios objective-c iphone orientation hierarchy

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

RootViewController - (模态segue)>图片集锦

问题出在我做的时候:

Rootviewcontroler - (模态segue)> ViewControllerModal - (模态segue)>图片集锦

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

RootViewController - (模态segue)> NavigationController A - (推送segue)> NavigationController B - (模态segue)>图片集锦

我不知道如何将层次结构导航到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;

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

我必须为一个电子商务应用程序做一次,并且必须呈现一个标志性的屏幕是景观,而应用程序的其余部分是纵向的。为了解决这个问题,首先我们在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;
    }
}

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

- (UIInterfaceOrientationMask) forcedOrientation {
    return UIInterfaceOrientationMaskLandscape;
}

-(void) viewDidAppear:(BOOL)animated {
...
    [self forceOrientationAdjustment]
...
}

虽然这种方法很完美,但对我来说总是感觉像是黑客。我们必须非常快速地呈现这个假视图控制器,以便获得改变的方向。可能有更好的解决方案,可能是这个:How to force a UIViewController to Portrait orientation in iOS 6

祝你好运!