繁体   English   中英

强制UIViewController方向

[英]Force UIViewController orientation

我做了一些研究,但似乎找不到答案来使我的navigationController的rootViewController在启动时是正确的。 我最初的问题是在这里: iPad的启动方向与横向方向不正确(上下颠倒)

在我的info.plist中,我将其设置为支持两种横向方向。 如果我将rootViewController更改为:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    return UIInterfaceOrientationIsLandscape(interfaceOrientation); // original that does not work
    return YES;
}

然后我的应用将以正确的方向启动。 但是,我不想支持人像模式。 我只想支持横向模式。 我以为可以通过执行以下操作来强制方向并防止其切换为纵向模式:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        UIDeviceOrientation orientation = UIInterfaceOrientationLandscapeLeft;
        [UIApplication sharedApplication].statusBarOrientation = orientation;
    }
    else {

    }

但这不会阻止应用程序旋转到纵向模式。 是否可以强制定位? 为了使启动方向正确(是否仅使用横向模式),我还需要做其他事情吗? 谢谢!

我刚刚创建了一个示例项目,将支持的界面方向(iPad)设置为“横向左”和“横向右”(在info.plist中)。

然后,我使用了:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
}

而且效果很好。 不管我如何旋转它都被迫景观。

您是否还有其他可见的视图控制器可能向所有方向返回YES? 这可能会使其混淆。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM