旋转已呈现的视图并锁定呈现视图控制器的方向

5

我正在开发一款仅支持横屏的iPad应用程序,我希望允许某些呈现的视图控制器支持所有方向,而不改变呈现视图控制器的方向。在Xcode设置中,我支持除了倒置方向之外的所有方向。

我使用以下代码来呈现视图控制器:

    ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
    vc.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:vc animated:YES completion:nil];

我使用的代码允许呈现视图控制器的方向:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return YES;
}

You Tube应用程序在播放视频时提供了相同类型的功能,您知道它是如何工作的吗?

非常感谢您的帮助。


你解决了这个问题吗? - Tometoyou
2个回答

0

AppDelegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

if(_isAllModes)
    return UIInterfaceOrientationMaskAll;
else
    return UIInterfaceOrientationMaskPortrait;
}

您的视图控制器。旋转:

[(AppDelegate*)([UIApplication sharedApplication].delegate) setIsAllModes:YES];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

0

我曾经遇到过类似的问题,但是项目设置会覆盖你在代码中应用的单个ViewController设置。为了解决这个问题,我在项目设置中只保留了最小可接受的方向,并在AppDelegate.m中进行了扩展,如下所示:

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window //newUX
{
    return UIInterfaceOrientationMaskAll;
}

无论如何,您都可以做得更好,例如,如果您想在一个特定场景中仅启用所有方向,请只说如果堆栈中的最后一个视图控制器是“全部” ,就像下面一样

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window //newUX
{
    if([self.navController.viewControllers.lastObject isKindOfClass:[MyAllOrientationVC class]])//newUX
        return UIInterfaceOrientationMaskAll;
    else
        return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
}

1
我没有使用导航控制器,如果我改变方向,它会同时改变两个视图控制器的方向,我只想要被呈现的视图控制器的方向改变,而不是呈现的视图控制器(因为我通过设置呈现样式表单表格来呈现视图控制器,所以两个视图控制器都可见)。 - user2493047

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接