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



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

您在播放视频时提供相同的功能,知道它是如何工作的吗?

任何帮助将不胜感激,谢谢。

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"];

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

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

无论如何,您都可以做得更好,例如,如果您只想在一个特定场景中启用所有方向,只需说堆栈中的最后一个 viewController 是否是"全部"-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;
}

最新更新