Search code examples
iosobjective-cuiviewcontrollerios11

In iOS 11 , UIViewController's transitionFromViewController never calls its completion block


Starting in iOS 11, UIViewController's transitionFromViewController:toViewController:duration:options:animations:completion: method appears to not call its completion block anymore.

Sample code snippet below:

[self addChildViewController:toVC];

[fromVC willMoveToParentViewController:nil];

[self transitionFromViewController:fromVC
toViewController:toVC
duration:0.4
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{}
completion:^(BOOL finished) {
    NSLog(@"Completion called"); // this completion is never executed
}];

This is causing me all kinds of issues in getting my views to transition and animate correctly. Has anyone else run into this behavior, and/or discovered a workaround?


Solution

  • So it turns out that I wasn't explicitly adding toVC.view as a subview to self.view after adding toVC as a child view controller to self. Odd that this behaves differently in iOS 11 vs. previous versions, but this did the trick:

    [self addChildViewController:toVC];
    [self.view addSubview:toVC.view]; // This line is what is needed
    
    [fromVC willMoveToParentViewController:nil];
    
    [self transitionFromViewController:fromVC
    toViewController:toVC
    duration:0.4
    options:UIViewAnimationOptionTransitionCrossDissolve
    animations:^{}
    completion:^(BOOL finished) {
        NSLog(@"Completion called");
    }];