Create a horizontal paging UIPageViewController programmatically
suggest change- Create the view controllers the page view controller will manage.
UIViewControllerhas noidentifierproperty, so add a base class and make each page inherit from it.indexbelow is anNSIntegerproperty 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];
- Create UIPageViewController instance.
UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:nil];
- The data source is this class, which must adopt
UIPageViewControllerDataSource.
pageViewController.dataSource = self;
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];
}
- 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];
- 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;
}
- 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 - 1on an empty array underflows ifcountis unsigned, so compare againstcountinstead.
- (UIViewController *)childViewControllerAtIndex:(NSInteger)index
{
if (index < 0 || index >= (NSInteger)[viewControllers count]) {
return nil;
}
return [viewControllers objectAtIndex:index];
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents