K.K.D
K.K.D

Reputation: 937

How to change the orientation of specific ViewController in Xcode

I want to change the orientation of specific ViewController in Xcode.

I make a,b,cViewController and change the orientation only cViewController to LandscapeRight. (a and b's orientation are Portrait)

But if I change the orientation on cViewController and move ViewController from c to b, b's orientation is also changed to LandscapeRight. (screen transition is push)

code:

a and bViewController's DidLoad

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

cViewController's DidLoad

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

How can I change the orientation only cViewController?

Upvotes: 1

Views: 391

Answers (1)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

step-1

create the one bool property in your appdelegate like , this

@property () BOOL restrictRotation;

and call the function

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

step-2

and import the appdelegate #import "AppDelegate.h" in C VC

on your C VC View Will appear, call like

-(void)viewWillAppear:(BOOL)animated{
// for rotate the VC to Landscape
[self restrictRotationwithNew:YES];
}

(void)viewWillDisappear:(BOOL)animated{
   // rotate the VC to Portait
  [self restrictRotationwithNew:NO];

[super viewWillDisappear:animated];
}


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

}

Choice 2

on your C VC check the Orientation using the delegate function of UIDeviceOrientationDidChangeNotification

 [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification  object:nil];



 - (void)orientationChanged:(NSNotification *)notification{


     [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];


}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation    {

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

switch (deviceOrientation) {
    case UIDeviceOrientationPortrait:

        NSLog(@"orientationPortrait");
       ;

        break;
    case UIDeviceOrientationPortraitUpsideDown:

        NSLog(@"UIDeviceOrientationPortraitUpsideDown");
        break;
    case UIDeviceOrientationLandscapeLeft:

        NSLog(@"OrientationLandscapeLeft");



        break;
    case UIDeviceOrientationLandscapeRight:

        NSLog(@"OrientationLandscapeRight");

        break;
    default:
        break;
}
}

Upvotes: 1

Related Questions