“viewWillTransitionToSize:” not called in iOS 9 when the view controller is presented modally

删除回忆录丶 提交于 2019-11-30 17:12:12

In your current view controller, if you override viewWillTransitionToSize:withTransitionCoordinator:, make sure you call super. Otherwise, this message won't get propagated to children view controllers.

For Objective-C:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // Your other code ... 

And Swift:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    // Your other code ...
}

May seem a trivial case, but when using an iPad check that the user has not activated rotation lock in the settings or control panel or side button

People have already explained that you have to call super. I'd like to add a piece of information that might help people who would have faced what i faced.

Scenario: Parent -> Child (viewWillTransition not called in child)


If your view controller is a child view controller, then check if the parent view controller delegate is called and if super is called there. Otherwise it won't be propagated to the child view controller!

class ParentViewController: UIViewController {

    func presentChild() {
        let child = ChildViewController()
        present(child, animated: false, compeltion: nil)
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator) // If this line is missing your child will not get the delegate call in it's viewWillTransition

        // Do something
    }
}

class ChildViewController: UIViewController {

    // This method will not get called if presented from parent view controller and super is not called inside the viewViewWillTransition available there.
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
       super.viewWillTransition(to: size, with: coordinator)

       //Do something
    }
}

P.S - This happened to me because i did not write the code for the parent.

Maybe it's a little late, but I put this here for anyone stumbling upon this frustrating issue.

Keep in mind that viewWillTransitionToSize:withTransitionCoordinator: sometimes gets called on the presentingViewController of the view controller you are expecting it to. (And if that view controller has a presentingViewController too, it may get called on that)

I couldn't really figure out the logic behind this, but that was the point in my case. So I had to override viewWillTransitionToSize:withTransitionCoordinator: in many of my view controllers like this:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

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