【问题标题】:iOS 7: Restrict device current orientation when modal view controller is presentediOS 7:出现模态视图控制器时限制设备当前方向
【发布时间】: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;
}

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

【问题讨论】:

    标签: cocoa ios7 popover screen-rotation


    【解决方案1】:

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

    -(NSUInteger)supportedInterfaceOrientations
    {
             return UIInterfaceOrientationMaskPortrait;
    }
    

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

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
    
       return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    
    }
    

    【讨论】:

    • 您的应用运行良好。当我使用 segue 呈现模态控制器时,我的应用程序也可以正常工作,但是当我使用 presentViewController 方法时它不起作用。
    • 用 presentViewCtrl 检查我的例子:dl.dropboxusercontent.com/u/19438780/testRotate-portrait2.zip
    • 您的应用再次运行良好。我想我一定是在某个地方犯了一些错误,会弄清楚的。不过谢谢你的帮助,至少现在我知道我的方法没有错。
    • 如果“以上”对您有所帮助,您可以接受答案(绿色小标志):) 编码愉快!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 2013-10-20
    • 2020-04-14
    • 2013-10-11
    相关资源
    最近更新 更多