iOS 7:出现模态视图控制器时限制设备当前方向

Posted

技术标签:

【中文标题】iOS 7:出现模态视图控制器时限制设备当前方向【英文标题】:iOS 7: Restrict device current orientation when modal view controller is presented 【发布时间】:2014-03-19 14:57:32 【问题描述】:

我有一个包含 UICollectionView 的 UIViewController。在点击任何 UICollectionViewCell 时,我会呈现一个模态视图控制器。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PopViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"DetailsView"];
vc.view.backgroundColor = [UIColor clearColor];
vc.transitioningDelegate = self;
vc.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:vc animated:YES completion:nil];

PopViewController 正确显示。现在我想在呈现 PopViewController 时限制设备方向。也就是说,如果 PopViewController 以纵向模式呈现,那么即使我切换到横向模式(在模拟器中使用向左或向右旋转),它也不应该变为横向,直到我关闭 PopViewController。

我在 PopViewController 中使用了以下代码:

-(BOOL)shouldAutorotate 
    return NO;

还需要什么(或替代)将弹出视图锁定到当前方向?

【问题讨论】:

【参考方案1】:

在你的模态控制器中尝试添加这个,也(ios > 6)

-(NSUInteger)supportedInterfaceOrientations

         return UIInterfaceOrientationMaskPortrait;

要支持 iOS 5 或更低版本,您必须额外添加:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation


   return (toInterfaceOrientation == UIInterfaceOrientationPortrait);


【讨论】:

来吧!!!试试这个例子dl.dropboxusercontent.com/u/19438780/testRotate-portrait.zip 您的应用运行良好。当我使用 segue 呈现模态控制器时,我的应用程序也可以正常工作,但是当我使用 presentViewController 方法时它不起作用。 用 presentViewCtrl 检查我的例子:dl.dropboxusercontent.com/u/19438780/testRotate-portrait2.zip 您的应用再次运行良好。我想我一定是在某个地方犯了一些错误,会弄清楚的。不过谢谢你的帮助,至少现在我知道我的方法没有错。 如果“以上”对您有所帮助,您可以接受答案(绿色小标志):) 编码愉快!

以上是关于iOS 7:出现模态视图控制器时限制设备当前方向的主要内容,如果未能解决你的问题,请参考以下文章

旋转设备时表格视图更新无效

如何使我的键盘弹出窗口更加流畅,我的视图控制器以模态方式出现?

当被模态视图控制器覆盖时,iOS 6 视图控制器布局在方向更改后不会更新

iiview 甲板控制器在呈现模态视图时更改框架而不更改界面方向

如何通知父视图控制器模态视图控制器中更改的屏幕方向?

iPad 模态视图控制器以纵向动作,即使它是横向的