禁止在iOS 8中为UIModalPresentationCustom modalPresentationStyle旋转

我用这个在iOS 8中呈现一个clearcolor UIViewController:

self.modalPresentationStyle = UIModalPresentationCustom; [_rootViewController presentViewController:self animated:NO completion:nil]; 

在这个UIViewController我设置

 - (BOOL)shouldAutorotate { return NO; } 

但是我可以旋转这个viewController时自我呈现,当我使用self.modalPresentationStyle =UIModalPresentationCurrentContext它不会clearColor但不能旋转。 对于UIModalPresentationCustom风格如何禁止旋转?

我也尝试在iOS 8 + UIModalPresentationCustom中呈现一个清晰的颜色UIViewController,但未能禁止自动旋转。

什么工作对我来说是使用UIModalPresentationOverFullScreen来代替和自动旋转方法按预期工作:

UIModalPresentationOverFullScreen

呈现视图覆盖屏幕的视图呈现样式。 演示完成后,所呈现内容下的视图不会从视图层次结构中删除。 所以如果呈现的视图控制器没有用不透明的内容填充屏幕,则底层内容显示通过。

在iOS 8.0及更高版本中可用。

我设法解决这个问题,在我的AppDelegate的UIViewController上添加这个类别。 不是最大的修复,但迄今还没有发现任何其他有用的东西。

 @implementation UIViewController (customModalFix) - (BOOL)shouldAutorotate { if ([self.presentedViewController isKindOfClass:[MyCustomPortraitOnlyClass class]]) { return [self.presentedViewController shouldAutorotate]; } return YES; } @end 

这个问题我迟到了,但遇到同样的问题,find了解决办法。

方法shouldAutorotate似乎工作不一致在iOS8(在iOS7工作很好)。 在7和8中完美的工作方式是supportedInterfaceOrientation。

在我的应用程序中的所有控制器都只是肖像,除了我以模态方式呈现它(与UIModalPresentationCustom)之一,并希望支持纵向和横向。 这是我做到的:

我的应用程序由NavigationController驱动。 由于UINavigationController在决定其childControllers如何旋转时需要控制,我将NavigationController分类并实现了旋转的方法:

 - (NSUInteger)supportedInterfaceOrientations { if (self.topViewController.presentedViewController && ![self.topViewController.presentedViewController isBeingDismissed]) { return self.topViewController.presentedViewController.supportedInterfaceOrientations; } return self.topViewController.supportedInterfaceOrientations; } 

因此,该方法查找一个呈现控制器,如果有一个检查是否正在被解雇,如果是,它会将调用转发给topViewController(呈现一个,只允许肖像)。 如果没有被解雇(意思是模态在已经出现之后正在旋转),我将呼叫转移到允许所有定向的模态。

最后,如果根本没有一个presentViewController,将调用转发给topController,在我的情况下,所有这些都默认返回UIInterfaceOrientationPortrait。

iOS 8问题

8中的问题是,当你有一个模式的VC呈现,然后旋转成风景,如果你然后解雇它回到topController应该只允许肖像,应用程序保持横向,除非你在全屏呈现模式模式。 为什么? 因为iOS 8仅在modalPresentationStyle = UIModalPresentationFullScreen时触发调用supportedInterfaceOrientation的系统方向检查。 这在iOS 7中不会发生。

iOS 8 Hack解决scheme

您可以在解散之前手动强制旋转。 我把它包装到一个非animation块中,以确保用户不必等到屏幕每次从风景中消除模态时都会返回到人像。 我在UIViewController上做了一个类来closures模块,它检查操作系统的版本,并执行8的破解。下面是代码:

  #import "UIViewController+Rotation.h" @implementation UIViewController (Rotation) - (void)dismissModalControllerAnimated:(BOOL)flag completion:(void (^)(void))completion { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { [UIView performWithoutAnimation:^{ NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; }]; } [self dismissViewControllerAnimated:flag completion:completion]; } @end