iOS 8 - UIWindow方向始终是人像

时间:2014-11-13 18:34:51

标签: ios swift screen screen-orientation

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

我已将项目文件中支持的方向更改为Landscape Left和Landscape Right。整个用户界面按预期显示在横向中,但是当我创建新的UIWindow时,它会以纵向显示。新UIWindow的帧与屏幕的帧完全匹配,因此我无法想象为什么/如何以纵向模式显示。

以下代码是我用来创建和显示新UIWindow的代码,它充当模态:

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不应该处于横向模式,因为应用只支持横向方向?

我非常感谢有关如何解决此问题的任何建议。

编辑:

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

1 个答案:

答案 0 :(得分:3)

编辑(2015年7月2日)

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

我的新解决方案使用presentViewController的标准UIViewController方法。我初始化UIViewController,将我的自定义模式View添加为UIViewController的子视图,然后使用约束定位模态View。像魅力一样工作!


原始答案

我离开了一段时间但决定今天回来。我最终将模态窗口旋转-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+!使用子视图时,请注意模式的widthheight值会切换,因为它以纵向显示。因此,如果您想要水平居中子视图,请使用模态窗口height和子视图width而不是两个视图' width

相关问题