简体   繁体   English

强制UIViewController方向

[英]Force UIViewController orientation

I did some research but cannot seem to find the answer to have my rootViewController of my navigationController be correct on start up. 我做了一些研究,但似乎找不到答案来使我的navigationController的rootViewController在启动时是正确的。 My original question was here: launch orientation of iPad is incorrect for landscape orientation (upside down) . 我最初的问题是在这里: iPad的启动方向与横向方向不正确(上下颠倒)

In my info.plist, I have it set to support both landscape orientations. 在我的info.plist中,我将其设置为支持两种横向方向。 If I change my rootViewController to be: 如果我将rootViewController更改为:

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

then my app starts in the right orientation. 然后我的应用将以正确的方向启动。 However, I do not want to support portrait modes. 但是,我不想支持人像模式。 I only want to support landscape modes. 我只想支持横向模式。 I thought I could force the orientation and prevent it from switching to portrait modes by doing something like this: 我以为可以通过执行以下操作来强制方向并防止其切换为纵向模式:

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

    }

But this does not prevent the app from being rotated to portrait mode. 但这不会阻止应用程序旋转到纵向模式。 Is it possible to force the orientation? 是否可以强制定位? Is there something else I need to do in order for the startup orientation to be correct (landscape mode only)? 为了使启动方向正确(是否仅使用横向模式),我还需要做其他事情吗? Thanks! 谢谢!

I just created a sample project, set my Supported interface orientations (iPad) to Landscape Left and Landscape Right (in info.plist). 我刚刚创建了一个示例项目,将支持的界面方向(iPad)设置为“横向左”和“横向右”(在info.plist中)。

I then used: 然后,我使用了:

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

and it works fine. 而且效果很好。 It's forced to landscape not matter how i rotate it. 不管我如何旋转它都被迫景观。

Do you have any other view controllers visible that might be returning YES to all orientations? 您是否还有其他可见的视图控制器可能向所有方向返回YES? This could confuse it. 这可能会使其混淆。

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

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