4
votes

I have embedded a UIViewController in a UINavigationController. The orientation of the view of this controller is set to Portait. When I push a new view on this UIViewController, which is landscape only, the new view is being shown portrait as well, instead of it's orientation landscape.

I have tried to subclass the UINavigationController and added the following methods like this:

- (BOOL)shouldAutorotate {
    return self.topViewController.shouldAutorotate;
}

- (NSUInteger)supportedInterfaceOrientations {
    return self.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return self.topViewController.preferredInterfaceOrientationForPresentation;
}

In the rootViewController (LoginViewController) I did this:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

In the pushViewController (A custom ViewController) I did this:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

-(BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}

I'm using a storyboard and a push segue between them. I know that the problem lies in the push segue which leads to an taking over of the orientation of the topviewcontroller which is portrait and the pushViewController is landscape. Does anyboy know workarounds?

Any help is thankfully appreciated. Else I should drop the navVC and perform a modal segue.

KR

1

1 Answers

2
votes

Try this code :

In AppDelegate.m class write below code.

#pragma mark Orientation Code
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations = UIInterfaceOrientationMaskAll;

    if (self.window.rootViewController) {
        UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presented supportedInterfaceOrientations];
    }
    return orientations;
}

And next if you don't want orientation of the particular class for example

Stop orientation viewController.m

#pragma mark Orientation
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

final thing to change project device orientation of project Target.

Ex : Project TARGETS --> Device Orientation -- > select All (Portrait, UpSide Down, Landscape Left, Landscape Right)