问题描述
我正在将iOS 6应用程序更新为iOS 8(iPad),并且我创建的所有新UIWindows
始终以纵向模式显示.该应用程序最初既支持纵向也支持横向,但现在仅支持横向.
I am updating an iOS 6 app to iOS 8 (iPad), and any new UIWindows
that I create are always showing up in Portrait mode. The app originally supported both the Portrait and Landscape orientations but now will only support Landscape.
我已将项目文件中支持的方向更改为横向左"和横向右".整个UI如预期那样以横向显示,但是当我创建一个新的UIWindow
时,它以纵向显示.新的UIWindow
框架与屏幕的框架完全匹配,因此我无法想象为什么/如何以纵向模式显示它.
I've changed the supported orientations in the project file to Landscape Left and Landscape Right. The entire UI shows up in landscape, as expected, but when I create a new UIWindow
, it shows up in portrait. The new UIWindow
's frame matches the screen's frame exactly, so I can't imagine why/how it is showing up in portrait mode.
以下代码是我用来创建和显示新的UIWindow
(用作模式)的代码:
The following code is what I am using to create and show the new UIWindow
, which acts as a modal:
var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds)
modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)
modalWindow.addSubview(customView)
modalWindow.makeKeyAndVisible()
我已经为此苦苦挣扎了几个小时.默认情况下,UIWindow
是否不应该处于横向模式,因为该应用程序仅支持横向方向?
I've been struggling with this for a few hours; shouldn't the UIWindow
be in landscape mode by default since the app only supports the Landscape orientation?
对于如何解决此问题的任何建议,我将不胜感激.
I would greatly appreciate any advice on how to resolve this issue.
我刚刚创建了一个仅支持"Landscape Left"和"Landscape Right"的新测试应用程序,该问题也同样出现在这里.这是一个错误吗?我似乎无法理解为什么UIWindow在横向模式下会认为该应用程序处于纵向模式.
I just created a new test app that only supports Landscape Left and Landscape Right, and the issue occurs there as well. Is this a bug? I can't seem to understand why the UIWindow would think the app is in Portrait mode when it's in Landscape mode.
推荐答案
编辑(2015年7月2日)
此答案在iOS 8.3+和iOS 8的早期版本中均无效.我不建议任何人使用它,特别是因为不能保证它可以在将来的iOS版本中使用.
This answer breaks in iOS 8.3+ and possibly previous versions of iOS 8. I DO NOT recommend that anyone uses it, especially since it is not guaranteed to work in future iOS versions.
我的新解决方案使用UIViewController
的标准presentViewController
方法.我初始化UIViewController
,将自定义模态View
添加为UIViewController
的子视图,然后使用约束定位模态View
.就像魅力一样!
My new solution uses the standard presentViewController
method of a UIViewController
. I initialize a UIViewController
, add my custom modal View
as a subview of the UIViewController
, and then position the modal View
using constraints. Works like a charm!
原始答案
我离开了一段时间,但决定今天回到它.我最终将模式窗口旋转-90度,因为它会自动旋转90度以纵向显示.我还对模态窗口的宽度和高度进行了少量计算,以支持iOS 7和iOS8.这是我的新代码:
I left this for a while but decided to come back to it today. I ended up rotating the modal window -90 degrees since it is automatically rotated 90 degrees to be displayed in portrait. I also added a small calculation for the modal window's width and height to support both iOS 7 and iOS 8. Here is my new code:
// Get the screen size
var screenSize:CGSize = UIScreen.mainScreen().bounds.size
// Use the larger value of the width and the height since the width should be larger in Landscape
// This is needed to support iOS 7 since iOS 8 changed how the screen bounds are calculated
var widthToUse:CGFloat = (screenSize.width > screenSize.height) ? screenSize.width : screenSize.height
// Use the remaining value (width or height) as the height
var heightToUse:CGFloat = (widthToUse == screenSize.width ? screenSize.height : screenSize.width)
// Create a new modal window, which will take up the entire space of the screen
var modalWindow:UIWindow = UIWindow(frame: CGRect(x: 0, y: 0, width: widthToUse, height: heightToUse))
modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)
modalWindow.addSubview(customView)
// Create a -90 degree transform since the modal is always displayed in portrait for some reason
var newTransform:CGAffineTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI / 2))
// Set the transform on the modal window
modalWindow.transform = newTransform
// Set the X and Y position of the modal window to 0
modalWindow.frame.origin.x = 0
modalWindow.frame.origin.y = 0
modalWindow.makeKeyAndVisible()
如上所述,这在iOS 7+和iOS 8+上都很好用!使用子视图时,请注意,模态的width
和height
值是切换的,因为它以纵向显示.因此,如果要水平居中放置子视图,请使用模式窗口的height
和子视图的width
而不是两个视图的width
.
As stated, this works great on both iOS 7+ and iOS 8+! When working with subviews, note that the modal's width
and height
values are switched since it's displaying in portrait. So, if you want to center a subview horizontally, use the modal window's height
and the subview's width
instead of both views' width
.
这篇关于iOS 8-UIWindow方向始终为纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!