我正在将 iOS 6 应用程序更新到 iOS 8 (iPad),以及任何新的应用程序UIWindows
我创建的总是以纵向模式显示。该应用程序最初支持纵向和横向方向,但现在仅支持横向。
我已将项目文件中支持的方向更改为“横向左”和“横向右”。正如预期的那样,整个用户界面以横向显示,但是当我创建一个新的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
由于应用程序仅支持横向方向,因此默认处于横向模式?
我将非常感谢任何有关如何解决此问题的建议。
EDIT:
我刚刚创建了一个新的测试应用程序,仅支持横向左和横向右,并且问题也发生在那里。这是一个错误吗?我似乎无法理解为什么 UIWindow 会认为应用程序在处于横向模式时处于纵向模式。
编辑(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+ 上都非常有效!使用子视图时,请注意模式的width
and height
由于它是纵向显示,所以值会被切换。因此,如果您想让子视图水平居中,请使用模态窗口的height
和子视图的width
而不是两种观点'width
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)