有关在UIViewController中管理方向的说明 [英] Clarifications on managing orientations in UIViewController
问题描述
我的配置如下:
UINavigationController
-> ViewController
其中ViewController
是导航控制器的根视图控制器.
where ViewController
is the root view controller for the navigation controller.
ViewController
可以模态显示另一个视图控制器ModalViewController
,如下所示.
ViewController
can present modally another view controller, ModalViewController
, like the following.
self.presentViewController(modalViewController, animated: true, completion: nil)
在ModalViewController
之内,我重写了以下方法.实际上,模态视图控制器只能在Landscape
模式下显示.
Within ModalViewController
I override the following method. The modal view controller in fact can be presented only in Landscape
mode.
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
然后我覆盖ViewController
的用于响应方向变化的方法.
I then override ViewController
's method for responding to orientation changes.
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
print("Size is \(size)")
}
我注意到的是,如果显示了模态视图控制器,则仅当ModalViewController
处于Landscape
模式时才记录ViewController
中的print
,而没有登录Portrait
.换句话说,当我旋转设备时,ModalViewController
应该只能显示Landscape
方向.在引擎盖下,即使看不到ViewController
(并且设备位于Portrait
中),该控制器也应该能够响应大小更改.情况并非如此,因为我看不到print
日志.
What I noticed is that if the modal view controller is presented, the print
in ViewController
is logged only if ModalViewController
is in Landscape
mode, while it is no logged in Portrait
. In other words, while I rotate the device, ModalViewController
should be able to display Landscape
orientations only. Under the hood, even if ViewController
is not visible (and the device is in Portrait
), this controller should be able to respond to size changes. This is not the case since I cannot see the print
log.
用例:
如果看不到ModalViewController
,则为打印日志
if ModalViewController
is not visible this is the print log
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)
这是正确的行为吗?我的目标是即使(处于打开状态ModalViewController
时)设备处于纵向模式,也要响应ViewController
的方向变化.有任何线索吗?
Is this one the correct behaviour? My goal is to respond to orientation changes for the ViewController
even if (when ModalViewController
is opened) the device is in Portrait mode. Any clue?
修改
基于 matt 评论.
如果ViewController不是最前面的视图控制器,则它没有响应方向更改"的业务.您只是偶然发现了您不应该感兴趣的实现细节或边缘情况.
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
是充当父视图控制器的复杂控制器.它有两个子代:PortraitViewController
和LandscapeViewController
.这些控制器以viewWillTransitionToSize
方法交换.每当ModalViewController
不可见(未显示)时,交换均以正确的方式进行.相反,当显示ModalViewController
时,交换仅在Landscape
模式下运行(请参见上面的日志).
ViewController
is a complex controller that acts as a parent view controller. It has two children: PortraitViewController
and LandscapeViewController
. These controllers are swapped in viewWillTransitionToSize
method. Whenever ModalViewController
is not visible (not presented) the swapping works in the correct manner. On the contrary, when ModalViewController
is presented, the swapping runs just for Landscape
mode (see logs above).
推荐答案
在我看来,您似乎只是在错误的时间尝试做某事.当模式视图控制器被关闭并且视图控制器的视图将重新出现时,您将收到大量警告(包括appear
和layout
事件),并且您可以根据当前方向做任何需要做的事情,然后再执行您的视图对用户可见.
It sounds to me as if you're just trying to do something at the wrong time. When the modal view controller is dismissed and your view controller's view is about to reappear, you'll get plenty of warning (including appear
and layout
events) and you can do whatever needs doing, based on the current orientation, before your view becomes visible to the user.
这篇关于有关在UIViewController中管理方向的说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!