iOS with NavigationController As Root Make, One Controller ONLY Landscape

104 views Asked by At

I have an app in which the root controller is a UINavigationController. Push one button, and it pushes another view controller. Push yet another and it starts an AVCaptureSession with Overlay. I want THAT view, and that view ONLY to be Landscape, and not portrait. I have looked at a ton of different tutorials, and nothing is working.

The closest I have gotten is:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

in the viewDidAppear. However, there are some issues. One, it immediately goes right back to Portrait. Two, the other views are still all able to be portrait and landscape and I don't want that. I need help, please.

2

There are 2 answers

0
Optimus On BEST ANSWER

In my app delegate, I use this code and make sure only portrait mode is selected:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if ([self.window.rootViewController.presentedViewController isKindOfClass:[SecondViewController class]])
    {
        SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController;

        if (secondController.isPresented)
        {
            return UIInterfaceOrientationMaskLandscape;
        }
        else return UIInterfaceOrientationMaskPortrait;
    }
    else return UIInterfaceOrientationMaskPortrait;
}
0
Pankaj K. On

yes you can make particular viewController as a landscape by placing bellow method in your appDelegate.

-(NSUInteger)application:(UIApplication *)application    supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
    return UIInterfaceOrientationMaskPortrait;
else
    return UIInterfaceOrientationMaskAll;
}

then take one bool property in your appDelegate

@property (nonatomic) BOOL restrictRotation; 

and then in view controller where you want to change orientation create appDelegate shared instance and allow restriction to change like bellow method

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
} 

now call this method from your view controller and change orientation as you want.

[self restrictRotation:NO];

    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

and the most important thing is to change orientation to portrait when your view will be disappear otherwise all the other viewController also become in a landscape mode.

-(void)viewWillDisappear:(BOOL)animated
{
    NSNumber *value = [NSNumber     numberWithInt:UIDeviceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    [self restrictRotation:YES];
} 

I hope this will help you.