您可以在 iOS 应用程序中全局禁用旋转吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10125050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:49:43  来源:igfitidea点击:

Can you disable rotation globally in an iOS app?

iosxcode

提问by sayguh

I have an app made up of a lot of view controllers... in the project summary I've set Portrait orientation as the only supported device orientation.

我有一个由许多视图控制器组成的应用程序……在项目摘要中,我将纵向设置为唯一支持的设备方向。

However, the app still gets messed up when turned sideways.

但是,当横向转动时,该应用程序仍然会变得一团糟。

My question is, is there a way to globally disable autorotation through app delegate or something?

我的问题是,有没有办法通过应用程序委托或其他方式全局禁用自动旋转?

Or do I have to go into all of my view controllers and add the "shouldAutorotateToInterfaceOrientation" method?

或者我是否必须进入我所有的视图控制器并添加“shouldAutorotateToInterfaceOrientation”方法?

Just don't want to miss adding it to one or something...

只是不想错过将它添加到一个或某物...

Thanks!

谢谢!

采纳答案by Haris Hussain

in root view controller's method:

在根视图控制器的方法中:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

set 'return NO';

设置“返回否”;

this should do for all the Views.

这应该适用于所有视图。

回答by Supahfly

In Info.plist expand "Supported interface orientations" and remove Landscape items to make your application only run in portrait mode.

在 Info.plist 中展开“支持的界面方向”并删除横向项目以使您的应用程序仅在纵向模式下运行。

回答by Vaibhav Saran

There are three kinds of Device orientation keys there in the info.plistnow.

现在有三种设备方向键info.plist

  1. Supported interface orientations (iPad)
  2. Supported interface orientations (iPhone)
  3. Supported interface orientations
  1. 支持的界面方向 (iPad)
  2. 支持的界面方向 (iPhone)
  3. 支持的界面方向

Third one is I think for non-universal apps and rest two above are for iPad and iPhone.

第三个是我认为用于非通用应用程序,其余两个用于 iPad 和 iPhone。

You should give them a try.

你应该试试他们。

enter image description here

在此处输入图片说明

回答by karim

After struggling to set in UIViewController's shouldAutorotateand supportedInterfaceOrientationmethods, with no success in iOS6, I found the most effective is to set it in app delegate.

在UIViewController的奋力集后 shouldAutorotatesupportedInterfaceOrientation方法,在iOS6的没有成功,我找到了最有效的是设置在app委托。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

However returning UIInterfaceOrientationMaskPortraitUpsideDownwas crashing my app. I dont know whats wrong I was doing!

但是返回使UIInterfaceOrientationMaskPortraitUpsideDown我的应用程序崩溃。我不知道我做错了什么!

回答by Glenn

If you're supporting iPad, then you SHOULD NOT uncheck the landscape orientations, as it will prevent your app from being accepted by Apple on App Store.

如果你支持的iPad,那么你不应该取消景观方向,因为它会阻止你的应用程序被接受苹果公司的App Store。

To prevent rotating before the app shows your first screen, put this inside your AppDelegate.m

为了防止在应用程序显示你的第一个屏幕之前旋转,把它放在你的 AppDelegate.m 中

This method works and tested in iOS 7.1 above.

此方法在上述 iOS 7.1 中有效并经过测试。

// G - fix for ipad.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

回答by James Wolfe

Haris Hussain's answer appears to be deprecated now, as of IOS 6, but there are new methods available for limiting/enabling rotation.

从 IOS 6 开始,Haris Hussain 的回答现在似乎已被弃用,但有新方法可用于限制/启用旋转。

Here are the methods listed in the UIViewController header:

以下是 UIViewController 标头中列出的方法:

// New Autorotation support.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);

Note that shouldAutoRotate doesn't seem to work if you start the app in an already rotated state!

请注意,如果您以已经旋转的状态启动应用程序,shouldAutoRotate 似乎不起作用!