【发布时间】:2014-08-31 12:32:50
【问题描述】:
假设我有一个视图控制器,称之为 ViewControllerPortrait,它被设计为仅以纵向模式显示。例如:
当用户将设备旋转为横向时,我不希望 ViewControllerPortrait 将自身重新定位为横向,但我确实想呈现一个新的全屏视图。我们将全屏视图控制器称为 ViewControllerLandscapeFull。例如:
我最不想看到的是:
我尝试这样做的方法是让窗口的 rootViewController 在获得 willRotateToInterfaceOrientation:duration: 时全屏显示 ViewControllerLandscapeFull
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[navigationViewController setNavigationBarHidden:YES animated:YES];
self.viewControllerPortrait = [[ViewControllerPortrait alloc] init];
self.viewControllerPortrait.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:self.viewControllerPortrait animated:NO completion:NULL];
然后在 ViewControllerLandscapeFull 我有:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return (UIInterfaceOrientation)[UIDevice currentDevice].orientation;
}
这很好用。由于 ViewControllerLandscapeFull “更喜欢”风景并且有一个 UIModalPresentationFullScreen modalPresentationStyle,它只会在风景中显示。通过为supportedInterfaceOrientations 返回landscape | portrait,当ViewControllerLandscapeFull 获得willRotateToInterfaceOrientation:duration: 并最终在其上调用dismissViewControllerAnimated:NO 时,允许设备旋转回纵向。
我遇到的唯一问题是,在旋转回纵向期间,您可以暂时看到横向的 ViewControllerPortrait,这就是我要避免的。
ViewControllerPortrait 确实实现了:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
我已经验证 UIKit 正在调用它,但它显然没有任何效果。 (文档说 supportedInterfaceOrientations 只在根视图控制器上调用,并且视图控制器全屏显示,所以我真的很惊讶 UIKit 调用它。)
FWIW,我正在使用 iOS 8 beta 5 SDK 并为 7/8 构建。
非常感谢。
【问题讨论】:
-
你的应用中有蚂蚁标签栏或导航栏吗?我的意思是您不想旋转的视图控制器是否在任何选项卡栏或导航控制器内?
-
是的,我上面所说的 ViewControllerPortrait 是 UINavigationController 的一部分(实际上是一个子类)。