iOS 9 支持的接口方向不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32782044/
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
iOS 9 supportedInterfaceOrientations not working
提问by Bryan
I have a UIViewController
with the following code:
我有UIViewController
以下代码:
- (BOOL) shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
I am not using a UINavigationController
. When this UIViewController
is being displayed, the device will still rotate to landscape. I am targeting iOS 9, what's the issue here?
我没有使用UINavigationController
. 当这个UIViewController
被显示,该装置仍将旋转为横向。我的目标是 iOS 9,这里有什么问题?
回答by Bryan
So the issue was that I had defined the allowed orientations in info.plist
which apparently overrides anything you do anywhere else throughout the project.
所以问题是我已经定义了允许的方向,info.plist
其中显然覆盖了你在整个项目中其他任何地方所做的任何事情。
To correct the issue I removed the entries from info.plist
and defined them in the project settings. Now everything works as expected.
为了解决这个问题,我从info.plist
项目设置中删除了条目并定义了它们。现在一切都按预期进行。
回答by Xingxing
I don't think Bryan's answer works,for changing the orientations in project settings also changes the info.plist
as @mrhangz commented.
我不认为 Bryan 的回答有效,因为更改项目设置中的方向也会改变info.plist
@mrhangz 评论的内容。
If the issue is iOS9 only,it is probably due to the new feature of iOS9 in iPad called Split view
.The iOS9 enable Split view
by default in particular iPad device,see Apple documents here.
如果问题仅是 iOS9,则可能是由于 iPad 中 iOS9 的新功能称为Split view
iOS9。iOS9Split view
在特定 iPad 设备上默认启用,请参阅此处的Apple 文档。
The
split view
forced your app to support all orientations in all view once adoptted.So if you set all orientations support in either info.plist
or target general setting,and then split view
is supported by default,which will ignore the orientation setting though supportedInterfaceOrientations
in your viewController and support all orientations.
split view
一旦
采用,强制您的应用程序支持所有视图中的所有方向。因此,如果您在任一info.plist
或目标通用设置中设置所有方向支持 ,然后split view
默认支持,这将忽略supportedInterfaceOrientations
视图控制器中的方向设置并支持所有方向。
As the document written,if you checked Requires full screen
in your target settings,then your app will not support split view
.Now you can control orientations in code again.
正如文档所写,如果您检查Requires full screen
了目标设置,那么您的应用程序将不支持split view
。现在您可以再次在代码中控制方向。
回答by Daniel Patriarca
In swift 5
在迅捷 5
The code below will lock the current view controller into portrait mode but still allow the other view controllers to transition to landscape. I do believe that you have to enable all the orientations at the project level and then turn then "off" using this method but am not sure if there is way to turn them back "on" one by one.
下面的代码将当前视图控制器锁定为纵向模式,但仍允许其他视图控制器转换为横向模式。我确实相信您必须在项目级别启用所有方向,然后使用此方法关闭然后“关闭”,但不确定是否有办法将它们一一“打开”。
private var _orientations = UIInterfaceOrientationMask.portrait
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get { return self._orientations }
set { self._orientations = .portrait }
}
A more thorough explanation of it all can be found here: The supportedInterfaceOrientations method doesn't override any method from its superclass
在这里可以找到对这一切的更彻底的解释: supportedInterfaceOrientations 方法不会覆盖其超类中的任何方法
回答by HongchaoZhang
For simplicity, for iPad, if Supported interface orientations (iPad)property in info.plist includes all the four orientations, with UIRequiresFullScreenproperty value as NO, iOS will treat your app as supporting split view. If an app supports split view feature, you can not disable it from rotating, at least by the ways above.
为简单起见,对于 iPad,如果info.plist 中的Supported interface orientations (iPad)属性包含所有四个方向,并且UIRequiresFullScreen属性值为NO,则 iOS 会将您的应用视为支持拆分视图。如果应用程序支持拆分视图功能,则不能禁用它的旋转,至少通过上述方式。
I have a detail answer here.
我在这里有一个详细的答案。
回答by hoogw
I have try many solution, but the correct answer with working solution is:
我尝试了很多解决方案,但工作解决方案的正确答案是:
ios 8 and 9, no need to edit info.plist.
ios 8 和 9,无需编辑 info.plist。
- (BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
UIInterfaceOrientationUnknown
UIInterfaceOrientationUnknown
The orientation of the device cannot be determined.
无法确定设备的方向。
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortrait
The device is in portrait mode, with the device held upright and the home button on the bottom.
设备处于纵向模式,设备直立放置,主页按钮位于底部。
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationPortraitUpsideDown
The device is in portrait mode but upside down, with the device held upright and the home button at the top.
设备处于纵向模式但倒置,设备直立,主页按钮位于顶部。
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeLeft
The device is in landscape mode, with the device held upright and the home button on the left side.
设备处于横向模式,设备直立,主页按钮位于左侧。
UIInterfaceOrientationLandscapeRight
UIInterfaceOrientationLandscapeRight
The device is in landscape mode, with the device held upright and the home button on the right side.
设备处于横向模式,设备直立,主页按钮位于右侧。