0

I'm doing this, trying to see in landscape just a gallery, but all I get to show the landscape orientation when I connect the photo gallery modally with the main ViewController as well:

RootViewController -(modal segue)> PhotoGallery

the problem is when I do:

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

it does not work, and neither works with a navigation controller:

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

I do not know how navigate the hierarchy up to the gallery within supportedInterfaceOrientationsForWindow as well:

- (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;

}

Thanks in advance.

Javier
  • 3
  • 3

1 Answers1

0

I had to do this for an e-commerce app once, and had to present a signature screen is landscape while the remainder of the app was in portrait. In order to pull this off, first we created a category on UIViewController to support forced orientation

@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

We then subclassed UINavigationController and overrode the following methods:

- (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;
    }
}

The final step was to add a forcedOrientation call in the view controller we wanted to present in landscape and add a call to forceOrientationAdjustment in our viewDidAppear method:

- (UIInterfaceOrientationMask) forcedOrientation {
    return UIInterfaceOrientationMaskLandscape;
}

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

While this worked perfectly, it always felt like a hack to me. We had to present this fake view controller really quickly in order to get the orientation to change. There is likely a better solution available, perhaps this one: How to force a UIViewController to Portrait orientation in iOS 6

Good luck!

Community
  • 1
  • 1