Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Completion block isn't called when updating UIPageViewController during rotation

I'm trying to update the current page in UIPageViewController during rotation as following:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSArray *controllers = @[someViewController];
    UIPageViewControllerNavigationDirection direction = UIPageViewControllerNavigationDirectionForward;

    [self.pageController setViewControllers:controllers direction:direction animated:YES completion:^(BOOL finished) {
        /// This completion block isn't called ever in this case.
    }];
}

But, for some reason, the completion block isn't called ever in this case, while it's called as expected when I call the same method with same parameters from other places.

Could anyone confirm this strange behavior, and if it's a bug in UIPageViewController or just a misuse of me?

Update

I found out that if I change the parameter animated to NO the completion block will be called as expected! So, it seems more to me that it's a bug with UIPageViewController.

like image 834
Hejazi Avatar asked Oct 09 '13 09:10

Hejazi


1 Answers

Having the same issue, found the following thing: if someViewController is the current visible controller or pageController.viewControllers?.first and animated flag is set to true, then setViewControllers is called without invoking the completion handler. The easiest way is to call setViewControllers with animated flag set to false. No animations will be shown, but the completion handler will be invoked anyway. But in most cases you need animations to be shown. Thus, the only possible workaround here is to check if the viewController to be set isn't the visible one. If it is, you should call setViewControllers with animated flag set to false:

let completionBlock: (Bool) -> Void
let isAnimated = (newController != pageController.viewControllers?.first)

pageController.setViewControllers([newController],
                                  direction: .forward,
                                  animated: isAnimated, 
                                  completion: completionBlock)

Hope that helps you!

like image 69
GYFK Avatar answered Oct 25 '22 05:10

GYFK