Jacob Ruth
Jacob Ruth

Reputation: 181

Determining when a UIPageViewController scrolls past last page

I'm attempting to dismiss a UIPageViewController which is presented modally when the user scrolls past the last page. Is there a way to tell when this happens? viewControllerAfterViewController is called when the last view controller is shown.

Edit: I've also tried to add UISwipeGestureRecognizers but they are not being called. I think this is because the UIPageViewController's gesture recognizer's disable them.

Upvotes: 3

Views: 1847

Answers (2)

user1055568
user1055568

Reputation: 1429

UIViewController:viewWillAppear:animated will get called when the user swipes to that page. So you could add a dummy last page, and dismiss the pageViewController when that gets called.

Upvotes: 1

Beau Nouvelle
Beau Nouvelle

Reputation: 7252

You didn't indicate what language you're using so here it is in Objective-C

You should have your viewControllerAfterViewController method set up to look something like this:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    NSUInteger index = [(CustomViewController *)viewController index];

    index++;

    if (index == 10) { // the number of controllers you have.
        return nil;
    }
    return [self viewControllerAtIndex:index]; // you view controller creation method.   
}

viewControllerAfterViewController is called whenever a user attempts to swipe through your pages. If the user tries to go beyond the last page, this method should return nil. When that happens, dismiss the pageViewController.

Upvotes: 0

Related Questions