简体   繁体   English

如何以编程方式锁定/解锁iPhone的方向锁定?

[英]how to lock/unlock orientation lock of iphone programmatically?

we know that these simple steps to lock/unlock the orientation of your iPhone 3G: 我们知道锁定/解锁iPhone 3G方向的这些简单步骤:

From any screen, double-tap the home button
Scroll as far left as you can on the multitasking dock
The first symbol to the left will be a circular arrow
Select this to either lock or unlock the orientation of your iPhone 3G

But how we can do this programatically ? 但是我们如何以编程方式做到这一点?

Are you asking if you can do this for your app or lock the orientation for the device itself? 您是在问是否可以针对您的应用执行此操作或锁定设备本身的方向? Seems to me you're asking for the latter and I would have to ask why you want to do that. 在我看来,您要的是后者,而我不得不问您为什么要这样做。 It's not possible to lock the orientation for the device, because that way it would be locked in portrait mode for other apps as well. 无法锁定设备的方向,因为那样一来,其他应用程序也将被锁定为纵向模式。

You can however only support the orientations you want yourself. 但是,您只能支持自己想要的方向。 A lot of apps only support portrait mode and games generally support landscape only. 许多应用程序仅支持纵向模式,而游戏通常仅支持横向模式。

You can set the supported device orientations of your app in XCode. 您可以在XCode中设置应用程序支持的设备方向。 At the viewcontroller. 在视图控制器处。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

Assuming you want to support both landscape orientations. 假设您要同时支持两种横向。

You can't do it programmatically -- it would be plain wrong for an app to change a setting that affects everything else. 您无法通过编程方式进行操作-应用更改更改会影响其他所有内容的设置,这是完全错误的。

In your own app, you can restrict the supported orientations by setting UISupportedInterfaceOrientations in your info.plist (see doc here ). 在您自己的应用中,您可以通过在info.plist设置UISupportedInterfaceOrientations来限制受支持的方向(请参见doc 此处 )。 You can also restrict orientation per view through shouldAutorotateToInterfaceOrientation (see doc here ) 您还可以通过shouldAutorotateToInterfaceOrientation限制每个视图的方向(请参见此处的文档)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM