使用NavigationController作为Root Make的iOS,仅一个控制器横向

时间:2016-07-30 04:17:34

标签: ios uinavigationcontroller autorotate rootviewcontroller shouldautorotate

我有一个应用程序,其中根控制器是UINavigationController。按下一个按钮,它会推动另一个视图控制器。推另一个,它启动了覆盖AVCaptureSession。我想要那个视图,那个视图只是风景,而不是肖像。我查看了大量不同的教程,但没有任何工作。

我最接近的是:

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

。但是,有一些问题。一,它立即回到肖像。二,其他观点仍然可以是肖像和风景,我不希望如此。我需要帮助。

2 个答案:

答案 0 :(得分:0)

在我的app委托中,我使用此代码并确保只选择了纵向模式:

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

答案 1 :(得分:0)

是的,您可以通过在appDelegate中放置波纹管方法将特定的viewController设置为横向。

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

然后在appDelegate中获取一个bool属性

@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];
} 

我希望这会对你有所帮助。