Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I present a view controller from another one:

- (void)showModalView {    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];    MySecViewController *mySecViewController = [storyboard instantiateViewControllerWithIdentifier:@"secController"];    mySecViewController.delegate = self;    [self presentViewController:mySecViewController animated:YES completion:nil]; } 

Then in the presented UIViewController, the method viewWillTransitionToSize:withTransitionCoordinator: is called in iOS 8 but not in iOS 9...

Thanks

like image 734
AppsDev Avatar asked Oct 01 '15 09:10

AppsDev


2 Answers

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 ... } 
like image 80
Yuchen Avatar answered Oct 19 '22 01:10

Yuchen


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.

like image 39
Rakesha Shastri Avatar answered Oct 19 '22 01:10

Rakesha Shastri