2017-02-19 43 views
0

Я хочу изменить ориентацию конкретного ViewController в Xcode.Как изменить ориентацию конкретного ViewController в Xcode

Я делаю a, b, cViewController и меняю ориентацию только cViewController на LandscapeRight. (ориентация a и b - Portrait)

Но если я изменил ориентацию на cViewController и переместил ViewController с c на b, ориентация b также изменилась на LandscapeRight. (экран перехода толчок)

код:

а и DidLoad bViewController в

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

cViewController-х DidLoad

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

Как я могу изменить ориентацию только cViewController?

+0

изменить это из 'DidLoad' в' DidAppear' –

ответ

1

шаг-1

создать свойство один Ьоо в вашем AppDelegate как, этот

@property() BOOL restrictRotation; 

и вызовите функцию

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

шаг-2

и импортировать AppDelegate #import "AppDelegate.h" в C VC

на вашем C VC View Появятся, называют как

-(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; 

} 

Вариант 2

на вашем C VC проверить ориентацию с помощью функции делегата от 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; 
} 
}