Create a horizontal paging UIPageViewController programmatically

suggest change
  1. Create the view controllers the page view controller will manage. UIViewController has no identifier property, so add a base class and make each page inherit from it. index below is an NSInteger property on the parent.
@interface BasePageViewController : UIViewController
@property (nonatomic, assign) NSInteger identifier;
@end

BasePageViewController *firstVC = [[BasePageViewController alloc] init];
firstVC.identifier = 0;
BasePageViewController *secondVC = [[BasePageViewController alloc] init];
secondVC.identifier = 1;
NSArray *viewControllers = [[NSArray alloc] initWithObjects:firstVC, secondVC, nil];
  1. Create UIPageViewController instance.
UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                                                                           navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                                         options:nil];
  1. The data source is this class, which must adopt UIPageViewControllerDataSource.
pageViewController.dataSource = self;
  1. setViewControllers:direction:animated:completion: displays the first page. Later pages come from the data source.
if (viewControllers.count) {
    [pageViewController setViewControllers:@[[viewControllers objectAtIndex:0]]
                                 direction:UIPageViewControllerNavigationDirectionForward
                                  animated:NO
                                completion:nil];
}
  1. Add the page view controller as a child so it receives the parent’s appearance and rotation events. Use the parent’s bounds so the child fills the parent.
[self addChildViewController:pageViewController];
pageViewController.view.frame = self.view.bounds;
[self.view addSubview:pageViewController.view];
[pageViewController didMoveToParentViewController:self];
  1. Implement UIPageViewControllerDataSource.
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController
{
    self.index = [(BasePageViewController *)viewController identifier] - 1;
    return [self childViewControllerAtIndex:self.index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    self.index = [(BasePageViewController *)viewController identifier] + 1;
    return [self childViewControllerAtIndex:self.index];
}

- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
    return [viewControllers count];
}

- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
    return self.index;
}
  1. Return the view controller for an index, or nil when the index is outside the array. A negative index must return nil so the page view controller knows there is no neighbor. count - 1 on an empty array underflows if count is unsigned, so compare against count instead.
- (UIViewController *)childViewControllerAtIndex:(NSInteger)index
{
    if (index < 0 || index >= (NSInteger)[viewControllers count]) {
        return nil;
    }
    return [viewControllers objectAtIndex:index];
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents