0

我有一个应用程序,其中根控制器是 UINavigationController。按一个按钮,它会推动另一个视图控制器。再推一个,它会启动一个带有 Overlay 的 AVCaptureSession。我想要那个视图,那个视图只能是横向的,而不是纵向的。我看过很多不同的教程,但没有任何效果。

我得到的最接近的是:

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

在 viewDidAppear 中。但是,也存在一些问题。一,它立即回到肖像。二,其他视图仍然可以是纵向和横向的,我不希望那样。我需要帮助,请。

4

2 回答 2

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;
}
于 2016-07-30T06:46:17.273 回答
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"]; 

最重要的是当您的视图将消失时将方向更改为纵向,否则所有其他视图控制器也会变为横向模式。

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

我希望这能帮到您。

于 2016-07-30T07:23:34.640 回答