有关在UIViewController中管理方向的说明 [英] Clarifications on managing orientations in UIViewController

查看:80
本文介绍了有关在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是充当父视图控制器的复杂控制器.它有两个子代:PortraitViewControllerLandscapeViewController.这些控制器以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).

推荐答案

在我看来,您似乎只是在错误的时间尝试做某事.当模式视图控制器被关闭并且视图控制器的视图将重新出现时,您将收到大量警告(包括appearlayout事件),并且您可以根据当前方向做任何需要做的事情,然后再执行您的视图对用户可见.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆