我有如下配置:
UINavigationController
-> ViewController
其中 ViewController
是导航 Controller 的 Root View Controller 。
ViewController
可以模态呈现另一个 View Controller ,ModalViewController
,如下所示。
self.presentViewController(modalViewController, animated: true, completion: nil)
在 ModalViewController
中,我覆盖了以下方法。模态视图 Controller 实际上只能在 Landscape
模式下呈现。
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
然后我覆盖 ViewController
的方法来响应方向变化。
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
print("Size is \(size)")
}
我注意到,如果呈现模态视图 Controller ,则仅当 ModalViewController
位于 中时,才会记录
模式,而未登录 ViewController
中的 print
>LandscapePortrait
。换句话说,当我旋转设备时,ModalViewController
应该只能显示Landscape
方向。在引擎盖下,即使 ViewController
不可见(并且设备处于 Portrait
),此 Controller 也应该能够响应大小变化。事实并非如此,因为我看不到 print
日志。
用例:
如果 ModalViewController
不可见,这是打印日志
Size is (1024.0, 768.0)
Size is (768.0, 1024.0)
Size is (1024.0, 768.0)
Size is (768.0, 1024.0)
当 ModalViewController
以模态方式呈现时
Size is (1024.0, 768.0)
Size is (1024.0, 768.0)
这是正确的行为吗?我的目标是响应 ViewController
的方向变化,即使(当 ModalViewController
打开时)设备处于纵向模式。有什么线索吗?
编辑
基于matt评论。
If ViewController is not the frontmost view controller it has no business "responding to orientation changes". You've merely stumbled across an implementation detail or edge case in which you should have no interest.
ViewController
是一个复杂的 Controller ,充当父 View Controller 。它有两个 child :PortraitViewController
和 LandscapeViewController
。这些 Controller 在 viewWillTransitionToSize
方法中交换。每当 ModalViewController
不可见(未显示)时,交换就会以正确的方式进行。相反,当出现 ModalViewController
时,交换仅针对 Landscape
模式运行(参见上面的日志)。
最佳答案
在我看来,您只是想在错误的时间做某事。当模态视图 Controller 被关闭并且您的 View Controller 的 View 即将重新出现时,您将收到大量警告(包括 appear
和 layout
事件),您可以做任何事情在您的 View 对用户可见之前,需要根据当前方向执行操作。
关于ios - 在 UIViewController 中管理方向的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38402839/