2017-02-19 55 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; 
} 
} 
相關問題