2017-02-19 35 views
0

我想更改Xcode中特定ViewController的方向。如何更改Xcode中特定ViewController的方向

我使a,b,cViewController只改变方向cViewController到LandscapeRight。 (a和b的方向是肖像)

但是,如果我更改cViewController的方向并将ViewController从c移动到b,则b的方向也会更改为LandscapeRight。 (画面转换推)

代码:

和bViewController的DidLoad

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中

与您的C VC查看导入的appdelegate #import "AppDelegate.h"用C VC

会出现,打电话一样

-(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; 
} 
} 
相关问题