问题描述
我正在使用AutoLayout在iPad应用程序上工作,如果用户启用某种模式(平视"模式),则我仅希望支持纵向(或纵向颠倒)方向,并且,如果设备处于横向模式,我想自动切换到纵向模式.
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:
Objective-C:
Objective-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中以编程方式设置设备方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!