iOS 8-UI窗口方向始终为纵向

iOS 8-UI窗口方向始终为纵向,ios,swift,screen,screen-orientation,Ios,Swift,Screen,Screen Orientation,我正在将iOS 6应用程序更新为iOS 8(iPad),我创建的任何新的UIWindows都会以纵向模式显示。该应用程序最初支持纵向和横向,但现在只支持横向 我已将项目文件中支持的方向更改为横向左侧和横向右侧。正如预期的那样,整个UI显示在横向视图中,但当我创建一个新的UIWindow时,它显示在纵向视图中。新的ui窗口的框架与屏幕的框架完全匹配,因此我无法想象它为什么/如何以纵向模式显示 以下代码是我用来创建和显示新的ui窗口,它充当一个模式: var modalWindow:UIWindow

我正在将iOS 6应用程序更新为iOS 8(iPad),我创建的任何新的
UIWindows
都会以纵向模式显示。该应用程序最初支持纵向和横向,但现在只支持横向

我已将项目文件中支持的方向更改为横向左侧和横向右侧。正如预期的那样,整个UI显示在横向视图中,但当我创建一个新的
UIWindow
时,它显示在纵向视图中。新的
ui窗口
的框架与屏幕的框架完全匹配,因此我无法想象它为什么/如何以纵向模式显示

以下代码是我用来创建和显示新的
ui窗口
,它充当一个模式:

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()
我已经为此挣扎了几个小时;
ui窗口
不应该默认处于横向模式,因为应用程序只支持横向方向吗

如果您能就如何解决这个问题提供任何建议,我将不胜感激

编辑:

我刚刚创建了一个新的测试应用程序,它只支持左横向和右横向,问题也出现在那里。这是虫子吗?我似乎无法理解为什么UIWindow在横向模式下会认为应用程序处于纵向模式。

编辑(2015年7月2日)

这个答案在iOS 8.3+和iOS 8的早期版本中出现。我不建议任何人使用它,特别是因为它不能保证在未来的iOS版本中工作

我的新解决方案使用了
UIViewController
的标准
presentViewController
方法。我初始化一个
UIViewController
,将我的自定义模式
视图添加为
UIViewController
的子视图,然后使用约束定位模式
视图
。工作起来很有魅力


原始答案

我离开了一会儿,但决定今天再回来。最后,我将模式窗口旋转了90度,因为它会自动旋转90度,以纵向显示。我还为模式窗口的宽度和高度添加了一个小计算,以支持iOS 7和iOS 8。这是我的新代码:

// 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+上都非常有效!使用子视图时,请注意,模式的
宽度
高度
值是切换的,因为它以纵向显示。因此,如果您希望将子视图水平居中,请使用模式窗口的
高度
和子视图的
宽度
,而不是两个视图的
宽度

您是仅支持iOS 8+还是支持iOS 7?如果你是,你能在模拟器上测试它吗?我怀疑它会起作用,我知道原因。部署目标是iOS 8.0,但我的计划是支持iOS 7+和iOS 8+。我刚刚尝试将部署目标更改为iOS 7.0,而
ui窗口仍处于纵向模式。这就是你所说的“那个模拟器”吗?也许退一步,你为什么要用UIWindows进行模态演示?在iOS7中,定制的模态表示提供了全新的API。我建议先检查一下:我使用UIWindows是因为我使用的是自定义视图,而不是单独的视图控制器。我还想将自定义视图放置在屏幕上我想显示的任何位置。不,这不是我所说的模拟器。您需要从Xcode设置->下载下载下载iOS 7.1模拟器。无论如何,问题是在iOS 8中,UIScreen和主窗口返回基于方向的边界和帧。以前,您总是在纵向中获取/设置方向,现在它取决于您的方向。我相信这就是问题所在。嗨,我也有同样的问题。我已经创建了一条自定义taost消息,并将其添加到自定义
ui窗口中。一切都很好。但它仅在iPad3(iOS 8.3)横向模式下失败。有什么想法吗?