2017-02-19 6 views
0

Xcodeで特定のViewControllerの向きを変更したいと思います。Xcodeで特定のViewControllerの向きを変更する方法

私は、a、b、cViewControllerを作成し、方向のみをcViewControllerをLandscapeRightに変更します。 (aとbの向きはポートレートです)

しかし、私はcViewControllerで方向を変更し、ViewControllerをcからbに移動すると、bの向きもLandscapeRightに変更されます。 (画面遷移がプッシュである)

コード:

Aと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内の1つのブールプロパティを作成し、この

@property() BOOL restrictRotation; 

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

ステップ-2

関数を呼び出します

とあなたのC VCビューのデリゲート関数を使って向きを確認し、あなたの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に

をappdelegate #import "AppDelegate.h"をインポート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; 
} 
} 
関連する問題