Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining orientation of parent view after modal view has been presented with different orientation

So I am developing an iPad app that supports only landscape mode except for on one modal view controller. The issue I am having is that once I present the modal view and change the orientation to portrait then dismiss the view, the parent view (which should only support landscape) is in portrait mode until I rotate the device in which it then goes back to landscape and stays that way. I have been beating myself up trying to figure out how to keep the parents view original orientation but haven't been able to find a solution.

I have the following code in my app delegate to allow orientation changes on only that single modal view (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;
}

From the parent class (PhotosViewController) I am calling :

GalleryPhotoViewController *gpView = [GalleryPhotoViewController new];
[self presentViewController:gpView animated:YES completion:nil];

Also in my parent (and other views) I have the following code to disallow portrait mode :

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

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

Any ideas on how I can keep the orientation on my parent view? I was thinking about possibly just programmatically changing the orientation on the parent in the viewWillAppear method once the modal is dismissed but then I wouldn't know what the previous orientation was, not to mention I haven't been able to find code to do this regardless for ios6.

EDIT/SOLUTION : So I found a solution and what I ended up doing was leaving the application:supportedInterfaceOrientationsForWindow: code and just adding the UINavigation subclass to the parent view that was presenting the modal view and everything worked as expected and the parent retained its original orientation while the modal was able to change freely.

In my 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

Thanks @matt for the suggestions.

like image 763
shokaveli Avatar asked May 02 '13 02:05

shokaveli


1 Answers

I think the problem is your use of application:supportedInterfaceOrientationsForWindow:. Instead, get rid of that, and start with a UINavigationController subclass and make that the class of the root view controller that is your navigation interface. Then:

  • In the UINavigationController subclass, return UIInterfaceOrientationMaskLandscape from supportedInterfaceOrientations.

  • In the presented (modal) view controller, return UIInterfaceOrientationMaskAll from supportedInterfaceOrientations.

like image 195
matt Avatar answered Oct 31 '22 19:10

matt