比方说,我有一个视图控制器,称之为ViewControllerPortrait,它被设计为只能在肖像模式下显示。例如: -如何在设备旋转过程中“锁定”视图的方向
当用户旋转设备的风景,我不想ViewControllerPortrait永远重新定位自己的风景,但我想提出一个新的全屏视图。我们称之为全屏视图控制器ViewControllerLandscapeFull。例如:
我再也不想看到的是这样的:
我试图做到这一点的方法是让窗口的RootViewController的存在ViewControllerLandscapeFull在当屏幕全屏时willRotateToInterfaceOrientation:duration:
[[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。
Muchas gracias。
你有蚂蚁tabbar或导航栏在你的应用程序?我的意思是你不想旋转的视图控制器在任何tabbar或navigationcontroller内? – Rashad 2014-08-28 06:45:20
是的,我所称的ViewControllerPortrait是UINavigationController(实际上是一个子类)的一部分。 – 2014-08-28 19:40:05