我有一个UINavigationController作为我在iOS 7和iOS 8上的UIWindow的根视图控制器 . 从它的一个视图控制器,我提出了一个具有交叉溶解演示风格的全屏模态视图控制器 . 这个模态视图控制器应该能够旋转到所有方向,并且它工作正常 .
问题是当设备以横向方向保持并且模态视图控制器被解除时 . 呈现模态的视图控制器仅支持纵向方向,并且我已确认UIInterfaceOrientationMaskPortrait返回到-application:supportedInterfaceOrientationsForWindow: . -shouldAutorotate也返回YES . 然而,在解除模态之后,呈现视图控制器的方向仍然是风景 . 在允许模态采取设备方向的同时,如何强制它保持纵向方向?我的代码如下:
应用代表:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
UINavigationController *navigationController = (UINavigationController *)self.deckController.centerController;
NSArray *viewControllers = [navigationController viewControllers];
UIViewController *top = [viewControllers lastObject];
if (top && [top presentedViewController]) {
UIViewController *presented = [top presentedViewController];
if ([presented respondsToSelector:@selector(isDismissing)] && ![(id)presented isDismissing]) {
top = presented;
}
}
return [top supportedInterfaceOrientations];
}
return (UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight);
}
呈现视图控制器:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
模态视图控制器:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortrait);
}
3 回答
如果模态控制器在解除之前处于横向方向,则呈现的ViewController可能不会返回到原点方向(纵向) . 问题是因为在控制器实际被解除之前调用了AppDelegate supportedInterfaceOrientationsForWindow方法,并且呈现的控制器检查仍然返回Landscape掩码 .
设置一个标志以指示是否显示(模态) presented 视图控制器 .
并且在提供的模态中,ViewController根据标志设置方向:当呈现模态ViewController时 - 返回Landscape . 当它被解雇然后返回肖像
最后一步 - 从你的AppDelegate调用模态呈现ViewController的方向 . 我只是检查当前呈现的ViewController并在其上调用supportedInterfaceOrientations
有关更多信息,请查看this link
我最终继承了UINavigationController并重写了它的旋转方法 . 以下解决方案适用于iOS 7,但我相信iOS 8 beta 5中存在一个错误,导致视图控制器的视图在横向偏移模式后缩小到屏幕高度的一半 .
UINavigationController子类:
此解决方案适用于iOS 8 .
问题描述
应用程序密钥窗口将UINavigationController的子类作为其rootViewController .
此NC子类禁止某些接口方向 .
NC堆栈中的某些视图控制器(VC1)以模态和全屏方式呈现另一个视图控制器(VC2) .
这个呈现的VC2允许比NC更多的接口方向 .
用户将设备旋转到NC禁止的方向,但由提供的VC2允许 .
用户驳回提供的VC2 .
VC1视图的帧不正确 .
设置和插图
UINavigationController的子类:
VC1初始外观和UI视图堆栈:
从VC1呈现VC2(该示例中的QLPreviewController):
显示VC2并将设备旋转为横向:
VC2被解雇,设备返回纵向模式,但NC堆栈保持在横向状态:
原因
Apple文档说明:
显然处理UINavigationController堆栈时存在一个错误 .
解决方案
通过提供我们自己的转换委托可以绕过此错误 .
BTTransitioningDelegate.h
BTTransitioningDelegate.m
在呈现VC时导入转换委托:
存储对实例的强引用:
在_888168中实例化:
适当时打电话: