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

后端 未结 4 1099
夕颜
夕颜 2021-01-01 14:44

I present a view controller from another one:

- (void)showModalView
{
   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@\"Main\" bundle:nil];
          


        
4条回答
  •  一整个雨季
    2021-01-01 15:11

    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 the code for the parent was written by someone else and they forgot to call super.

提交回复
热议问题