我如何编程设置设备的方向在iOS7? [英] How do I programmatically set device orientation in iOS7?
问题描述
我工作的一个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;
}
基于我在其他地方看到这里的答案
,答案的看起来的是,我应该使用应用程序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.
事实上,当在横向模式下运行上面的code后,尝试以编程方式设置方向,当我查询应用程序statusBarOrientation具有以下code,它仍然是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触发的,所以我尝试后添加此code,没有效果:
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];
我意识到苹果希望在用户的手中离开设备的方向。不过,我想能支持横向模式时不单挑模式。
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"];
斯威夫特:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
我把它称为 - viewDidAppear:
这篇关于我如何编程设置设备的方向在iOS7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!