Search code examples
iosobjective-cgestureuipageviewcontroller

How to prevent PageViewController to overflow?


I am currently developing an iOS project that is based in a Page View Controller. Everything works fine, however, I would like to prevent the user to go beyond the limits of the pages.

In example, when the PageViewController is positioned in the first page, if the user tries to go to the previous page (even thought there is no one) by dragging the Page View to the left, the Root View Controller appears until the drag is released, as you can see in the screenshot below.

enter image description here

Is there any way to block this gesture in the first and the last page of the Page View Controller?


Solution

  • Using the PageviewController delegate method

    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    
    if (indexTest < NUM_OF_PAGES) {
        switch (indexTest) {
    
            case 0:{
                NSLog(@"Page AFTER is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:1] ] );
                indexTest++;
                return [viewControllers objectAtIndex:1];
                break;
            }
            case 1:{
                NSLog(@"Page AFTER is Page: %@", [NSString stringWithFormat:@"%@",[viewControllers objectAtIndex:2] ] );
                indexTest++;
                return [viewControllers objectAtIndex:2];
                break;
            }
            case 2:{
                NSLog(@"No pages AFTER this current page %d", indexTest);
                break;
            }
            default:{
                NSLog(@"PROBLEM  in viewAFTER, indexTest = %d!!!!", indexTest);
                break;
            }
        }
    
    }
    return nil;
    

    }

    do something like this , with indexTest is the int variable which increase the page count as per the swipe.

    NUM_OF_PAGES is the total pages available for page controlling.