如何在 iOS 7 中以编程方式设置设备方向? [英] How do I programmatically set device orientation in iOS 7?

查看:30
本文介绍了如何在 iOS 7 中以编程方式设置设备方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 iPad 应用程序,使用 AutoLayout,如果用户启用某种模式(抬头"模式),我只想支持纵向(或纵向颠倒)方向,此外,如果设备处于横向,我想自动切换到纵向模式.

I am working on an iPad app, using AutoLayout, where if the user enables a certain mode ("heads-up" mode), I want to support only portrait (or portrait upside down) orientation, and furthermore, if the device is in landscape, I'd like to automatically switch to portrait mode.

在顶视图控制器中,我有以下内容:

In the top view controller, I have the following:

- (NSUInteger) supportedInterfaceOrientations {
    if (self.modeHeadsUp) {
        return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (BOOL) shouldAutorotate {
    return TRUE;
}

根据我在这里其他地方看到的答案,答案似乎是我应该使用application setStatusBarOrientation".因此,在用户选择单挑"模式的方法中,我包含了:

Based on answers I've seen elsewhere here, the answer seems to be that I should use "application setStatusBarOrientation". Therefore, in the method where the user has selected "heads-up" mode, I have included:

    UIApplication *application = [UIApplication sharedApplication];
    [application setStatusBarOrientation:UIInterfaceOrientationPortrait
                                animated:YES];

然而,这似乎根本没有任何作用.虽然我可以通过物理方式移动设备以使其旋转为纵向,但它不会自动执行此操作.

However, this simply doesn't seem to do anything. While I can physically move the device to get it to rotate into portrait, it doesn't do so automatically.

实际上,在运行上述代码后在横向模式下尝试以编程方式设置方向时,当我使用以下代码查询应用程序statusBarOrientation"时,横向仍为4":

In fact, when in landscape mode after running the above code to attempt to programmatically set the orientation, when I query the application "statusBarOrientation" with the following code, it remains at "4" for landscape:

UIApplication *application = [UIApplication sharedApplication];
int orientation = [application statusBarOrientation];
self.movesTextView.text = [NSString stringWithFormat:@"ORIENTATION %d", orientation];

似乎没有使用 setStatusBarOrientation 触发自动布局,所以我尝试在之后添加此代码,但没有效果:

It seemed like maybe autolayout wasn't being triggered with the setStatusBarOrientation, so I attempted to add this code after, to no effect:

    [super updateViewConstraints];
    [self.view updateConstraints];

我意识到 Apple 想要将设备方向掌握在用户手中.但是,我希望能够在不处于单挑"模式时支持横向模式.

I realize Apple wants to leave device orientation in the hands of the user. However, I'd like to be able to support landscape mode when not in "heads-up" mode.

我是否缺少能够强制改变方向的东西?

Am I missing something to be able to force orientation change?

推荐答案

对于 iOS 7 &8:

For iOS 7 & 8:

目标-C:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];

斯威夫特 3+:

let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

我在 -viewDidAppear: 中调用它.

这篇关于如何在 iOS 7 中以编程方式设置设备方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆