相关疑难解决方法(0)

如何在iOS 7中以编程方式设置设备方向?

我正在使用AutoLayout在iPad应用程序上工作,如果用户启用某种模式("抬头"模式),我想只支持纵向(或纵向颠倒)方向,此外,如果设备在风景,我想自动切换到纵向模式.

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

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

- (BOOL) shouldAutorotate {
    return TRUE;
}
Run Code Online (Sandbox Code Playgroud)

基于我在其他地方看到的答案,答案似乎是我应该使用"application setStatusBarOrientation".因此,在用户选择"抬头"模式的方法中,我包括:

    UIApplication *application = [UIApplication sharedApplication];
    [application setStatusBarOrientation:UIInterfaceOrientationPortrait
                                animated:YES];
Run Code Online (Sandbox Code Playgroud)

但是,这根本就没有做任何事情.虽然我可以物理移动设备以使其旋转为纵向,但它不会自动移动.

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

UIApplication *application = [UIApplication sharedApplication];
int orientation = [application statusBarOrientation];
self.movesTextView.text = [NSString stringWithFormat:@"ORIENTATION %d", orientation];
Run Code Online (Sandbox Code Playgroud)

好像autolayout可能没有被setStatusBarOrientation触发,所以我试图在之后添加这个代码,没有效果:

    [super updateViewConstraints];
    [self.view updateConstraints];
Run Code Online (Sandbox Code Playgroud)

我意识到Apple希望将设备方向放在用户手中.但是,我希望能够在不进行"抬头"模式时支持横向模式.

我错过了能够强制改变方向的东西吗?

xcode autorotate ios autolayout ios7

71
推荐指数
4
解决办法
10万
查看次数

标签 统计

autolayout ×1

autorotate ×1

ios ×1

ios7 ×1

xcode ×1