Clarifications on managing orientations in UIViewController

≯℡__Kan透↙ 提交于 2019-12-23 03:32:11

问题


I have a configuration like the following:

UINavigationController
    -> ViewController

where ViewController is the root view controller for the navigation controller.

ViewController can present modally another view controller, ModalViewController, like the following.

self.presentViewController(modalViewController, animated: true, completion: nil)

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
}

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)")
}

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.

Use case:

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)

when ModalViewController is presented modally

Size is (1024.0, 768.0)
Size is (1024.0, 768.0)

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?

Edit

Based on matt comment.

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 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).


回答1:


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.



来源:https://stackoverflow.com/questions/38402839/clarifications-on-managing-orientations-in-uiviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!