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

时间:2017-07-18 13:33:12

标签: ios objective-c iphone ipad orientation

我正在开发仅支持横向的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;
}

你管应用在播放视频时提供相同的功能,任何想法它是如何工作的?

感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

AppDelegate.m

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

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

您的ViewController。旋转:

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

答案 1 :(得分:0)

我遇到了类似的问题,但项目设置会覆盖您以编程方式应用的单个ViewController设置。我为解决这个问题所做的是在项目设置中留下唯一最小的可接受的方向,并在AppDelegate.m中扩展它,如下所示

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

你无论如何都可以做得更好,例如,如果你想只在一个特定场景中启用所有方向,只需说明堆栈中的最后一个viewController是否为“all”-one,如下所示

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