iOS 6 强制设备横屏显示方向

57

我的应用程序中有10个视图控制器,我使用导航控制器来加载/卸载它们。

除了一个视图控制器以外,其余的所有视图控制器都是竖屏模式。假设第7个视图控制器是横屏模式,当它被加载时我需要它以横屏模式呈现。

请建议一种方法来强制在IOS 6中从纵向模式转换为横向模式(最好在IOS 5中也适用)。

这是我在IOS 6之前的操作方法:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIViewController *c = [[[UIViewController alloc]init] autorelease];
    [self presentModalViewController:c animated:NO];
    [self dismissModalViewControllerAnimated:NO];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

弹出和关闭模态视图控制器会迫使应用程序重新审视其方向,因此shouldAutorotateToInterfaceOrientation方法会被调用。

我在iOS 6中尝试的内容:

- (BOOL)shouldAutorotate{
    return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationLandscapeLeft;
}
加载时,控制器一直呈现为横屏状态。在旋转设备后,方向就会正常改变。但是我需要让控制器在加载时自动旋转到横屏状态,这样用户就必须旋转设备才能正确查看数据。
另一个问题:在将设备旋转回纵向后,虽然我已经在supportedInterfaceOrientations中指定了UIInterfaceOrientationMaskLandscape,但方向仍然保持为纵向。为什么会这样呢?
此外,以上3种方法都没有被调用。 一些有用的数据:
  1. 在我的plist文件中,我指定了除倒置之外的3个方向。
  2. 该项目是在Xcode 4.3 IOS 5中启动的。所有类(包括xib文件)都是在Xcode 4.5 IOS 6之前创建的,现在我使用的是最新版本。
  3. 在plist文件中,状态栏设置为可见。
  4. 在要在横屏中显示的xib文件中,状态栏为“无”,方向设置为横屏。
感谢任何帮助。谢谢。

我有和你一样的问题,你解决了吗? - aneuryzm
我讨厌新的iOS 6。我必须更改所有我的应用程序,以使方向正确工作。 - Bagusflyer
5
嗨,@bagusflyer,这就是程序员的生活啊))) - John Smith
1
这个问题和我的问题很像,我已经解决了。解决方案在这里:http://stackoverflow.com/questions/14658268/why-cant-i-force-landscape-orientation-when-use-uinavigationcontroller - Bob
你的问题对我来说是一个解决方案。 我也想要同样的结果,我刚刚写了你的ios6代码,它可以自动加载横向视图。 - Shilpa
这是我针对你在iOS 7中的问题提供的解决方案:https://dev59.com/T33aa4cB1Zd3GeqPhcDg#22491787 - Fede Cugliandolo
13个回答

0

我通过子类化UINavigationController并覆盖导航控制器的supportedInterfaceOrientations来解决了这个问题,代码如下:

- (NSUInteger)supportedInterfaceOrientations
{
     return [[self topViewController] supportedInterfaceOrientations];
}

所有实现的控制器都使用其所需的方向支持了supportedInterfaceOrientations。


0

前往您的 Info.plist 文件并进行更改 在此输入图片描述


-1
我使用了以下解决方案。在一个视图控制器中,它的方向与所有其他视图控制器不同,我在 prepareForSegue 方法中添加了一个方向检查。如果目标视图控制器需要与当前显示的界面方向不同的界面方向,则会发送一条消息,强制在 seque 过程中旋转界面。
#import <objc/message.h>

...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if(UIDeviceOrientationIsLandscape(self.interfaceOrientation))
    {
        UIInterfaceOrientation destinationOrientation;

        if ([[segue destinationViewController] isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *navController = (UINavigationController *)[segue destinationViewController];
            destinationOrientation = [navController.topViewController preferredInterfaceOrientationForPresentation];
        } else
        {
            destinationOrientation = [[segue destinationViewController] preferredInterfaceOrientationForPresentation];
        }

        if ( destinationOrientation == UIInterfaceOrientationPortrait )
        {
            if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
            {
                objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait );
            }
        }
    }
}

3
使用私有API从来不是解决方案。 - Javier Soto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接