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

1

16

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.

Ardath answered 2/5, 2013 at 2:39 Comment(0)
B
12

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.

Brett answered 2/5, 2013 at 2:55 Comment(5)
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. Thanks for the help.Ardath
@matt: I set above you said but still I got portrait while dismiss the view controller. What should I need to set in parent viewOberon
@Oberon You can't really show your code in a comment. Ask a separate question! That way you can show clearly what you're actually doing.Brett
@Brett Are you able to help me on this #38403339? ThanksPericarditis
I'm seeing a bug right now where the situation is the opposite: parent vc is a view controller supporting all but upsidedown, the modal vc supports portrait only. If the device is held vertically during the modal vc (the UI is still fixed portrait), then when dismissing the vc the parent vc's UI "breaks", there seems to be some apparent UI bug in UIKit and/or auto layout. Anyone?Sin

© 2022 - 2024 — McMap. All rights reserved.