0

我有一個應用程序,其中根控制器是一個UINavigationController。按下一個按鈕,並推動另一個視圖控制器。推另一個,它開始與Overlay的AVCaptureSession。我想要這個視圖,而這個視圖只能是風景,而不是肖像。我看了很多不同的教程,並沒有任何工作。iOS與NavigationController作爲根製作,一個控制器只有景觀

我得到的最接近的是:

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

。但是,有一些問題。一,它立即回到肖像。其二,其他意見仍然可以是肖像和風景,我不想那樣。我需要幫助,請。

回答

0

在我的應用程序的委託,我用這個代碼,並確保選中只肖像模式:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
    if ([self.window.rootViewController.presentedViewController isKindOfClass:[SecondViewController class]]) 
    { 
     SecondViewController *secondController = (SecondViewController *) self.window.rootViewController.presentedViewController; 

     if (secondController.isPresented) 
     { 
      return UIInterfaceOrientationMaskLandscape; 
     } 
     else return UIInterfaceOrientationMaskPortrait; 
    } 
    else return UIInterfaceOrientationMaskPortrait; 
} 
0

是你可以通過將波紋管的方法在你的appDelegate使特定的viewController作爲風景。

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

再取一個布爾值屬性在你的appDelegate

@property (nonatomic) BOOL restrictRotation; 

,然後在視圖控制器要改變方向創建的appDelegate共享實例,並允許限制來改變像波紋管方法

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

現在從您的視圖控制器調用此方法,並根據需要更改方向。

[self restrictRotation:NO]; 

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

和最重要的事情是要改變方向爲縱向時,您的視圖會消失,否則所有其他的viewController也成爲一道風景模式。

-(void)viewWillDisappear:(BOOL)animated 
{ 
    NSNumber *value = [NSNumber  numberWithInt:UIDeviceOrientationPortrait]; 
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 
    [self restrictRotation:YES]; 
} 

我希望這會幫助你。

相關問題