how to show spine in uipageviewcontroller? - ios

I am creating a simple UIPageViewController with just two pages, how do I show the spine? Currently there is nothing in the middle. Also the outside borders are missing.
This is the code for UIPageViewController
//
// PageViewController.m
// pageuiviewproject
//
//
#import "PageViewController.h"
#import "contentViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface PageViewController ()
#property (nonatomic, strong) UIPageViewController *pageViewController;
#end
#implementation PageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
NSArray *viewControllers = [NSArray arrayWithObject:[[contentViewController alloc]init]];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
self.pageViewController.view.layer.borderColor = [UIColor blackColor].CGColor;
self.pageViewController.view.layer.borderWidth = 3.0f;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UIPageViewController delegate methods
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
return [[contentViewController alloc] init];
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
return [[contentViewController alloc] init];
}
-(UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation{
NSArray *viewControllers = [NSArray arrayWithObjects:[[contentViewController alloc] init], [[contentViewController alloc] init], nil];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];
self.pageViewController.doubleSided = YES;
return UIPageViewControllerSpineLocationMid;
}
#end
The contentViewController is currently empty. This is what it looks like in simulator ...image
I want to create the following look mosaic app look. Is this a custom look? I was hoping this would be possible by setting UIPageViewController properties.

UIPageViewController does not provide any "look" at all. It's just a way of showing successive view controller's views. What you put in those views, or what you show behind/around/in front of the page view controller's view, is up to you. You want a book, draw a book.

Related

UIPageViewController with array of programatically generated views

So i need to create a UIPageViewController that shows a few views (1-25). Basically i have a quiz app, and for every wrong question i save the question number (1-25) in a NSMutableArray. After the quiz is done i want to show the user which quiz answer was wrong. I have the methods that sets the view of the wrong answers (same methods i use to set the views in the quiz).
This keeps giving me 'Thread1:' errors.
PVCPagesViewController.h
#import <UIKit/UIKit.h>
#import "PVCContentViewController.h"
#interface PVCPagesViewController : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
#end
.
PVCPagesViewController.m
#import "PVCPagesViewController.h"
#interface PVCPagesViewController () {
NSArray *pages;
}
#property (retain, nonatomic) NSArray *pages;
#property (strong, nonatomic) UIPageViewController *pageController;
#end
#implementation PVCPagesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//I think my problem is around here?
PVCContentViewController *page1 = [[PVCContentViewController alloc] init];
[page1 example1];
PVCContentViewController *page2 = [[PVCContentViewController alloc] init];
[page1 example2];
PVCContentViewController *page3 = [[PVCContentViewController alloc] init];
[page1 example3];
PVCContentViewController *page4 = [[PVCContentViewController alloc] init];
[page1 example2];
// load the view controllers in our pages array
self.pages = [[NSArray alloc] initWithObjects:page1, page2, page3, page4, nil];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[self.pageController setDelegate:self];
[self.pageController setDataSource:self];
[[self.pageController view] setFrame:[[self view] bounds]];
NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:0]];
[self.pageControl setCurrentPage:0];
[self.pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:self.pageController];
[self.view addSubview:self.pageControl];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
[self.view sendSubviewToBack:[self.pageController view]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
NSUInteger currentIndex = [self.pages indexOfObject:viewController]; // get the index of the current view controller on display
[self.pageControl setCurrentPage:self.pageControl.currentPage+1]; // move the pageControl indicator to the next page
// check if we are at the end and decide if we need to present the next viewcontroller
if ( currentIndex < [self.pages count]-1) {
return [self.pages objectAtIndex:currentIndex+1]; // return the next view controller
} else {
return nil; // do nothing
}
}
- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
NSUInteger currentIndex = [self.pages indexOfObject:viewController]; // get the index of the current view controller on display
[self.pageControl setCurrentPage:self.pageControl.currentPage-1]; // move the pageControl indicator to the next page
// check if we are at the beginning and decide if we need to present the previous viewcontroller
if ( currentIndex > 0) {
return [self.pages objectAtIndex:currentIndex-1]; // return the previous viewcontroller
} else {
return nil; // do nothing
}
}
- (void)changePage:(id)sender {
UIViewController *visibleViewController = self.pageController.viewControllers[0];
NSUInteger currentIndex = [self.pages indexOfObject:visibleViewController];
NSArray *viewControllers = [NSArray arrayWithObject:[self.pages objectAtIndex:self.pageControl.currentPage]];
if (self.pageControl.currentPage > currentIndex) {
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
} else {
[self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
}
}
#end
.
PVCContentViewController.h
#import <UIKit/UIKit.h>
#import "PVCPagesViewController.h"
#interface PVCContentViewController : UIPageViewController
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
#property (weak, nonatomic) IBOutlet UILabel *label3;
-(void)example1;
-(void)example2;
-(void)example3;
#end
And lastly:
PVCContentViewController.m
#import "PVCContentViewController.h"
#interface PVCContentViewController ()
#end
#implementation PVCContentViewController
#synthesize label3,label2,label1;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
-(void)example1{
label1.text = #"View 1";
label2.text = #"View 1";
label3.text = #"view 1";
}
-(void)example2{
label1.text = #"View 2";
label2.text = #"View 2";
label3.text = #"view 2";
}
-(void)example3{
label1.text = #"View 3";
label2.text = #"View 3";
label3.text = #"view 3";
}
#end
I have borrowed the start source code from here:
https://github.com/hackin247/UIPageViewController
My source code:
https://github.com/4FunAndProfit/UIPageViewControllerHelp
Please let me know if you need anymore info!
Have you tried setting the pageController with the self.pages array directly?
[self.pageController setViewControllers:pages direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
I don't see the point of creating yet another array(viewControllers). Maybe this is causing issues when trying to get the next page since the pageController only has a single viewController ([self.pages objectAtIndex:0]) but the "data source" says it should have more based on the self.pages.count.

ISO: Images not showing up in a page view controller app

I am new to iOS development, and I am having some issues showing images in a simple page view controller photo gallery.
The problem I am having is that the images in the child view won't be displayed. The only thing showing is a blank page.
Here is the view controller file holding page view controller:
#import "RTPGalleryViewController.h"
#import "RTPSinglePhotoViewController.h"
#import "JMImageCache.h"
#interface RTPGalleryViewController ()
#end
#implementation RTPGalleryViewController
#synthesize photosArray, pageViewController, pagesArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
pagesArray = [[NSMutableArray alloc] init];
[self generatePhotoViews];
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageViewController.dataSource = self;
[[self.pageViewController view] setFrame:[[self view] bounds]];
RTPSinglePhotoViewController *initialViewController = [pagesArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:initialViewController];
[self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 30);
[self addChildViewController:self.pageViewController];
[[self view] addSubview:[self.pageViewController view]];
[self.pageViewController didMoveToParentViewController:self];
// Do any additional setup after loading the view.
}
-(void) initWithPhotoArray:(NSMutableArray *) photos{
self.photosArray = photos;
}
-(void) generatePhotoViews{
for (int i=0; i< [photosArray count]; i++) {
RTPSinglePhotoViewController *view = [[RTPSinglePhotoViewController alloc] init] ;
view.imageUrl = [photosArray objectAtIndex:i];
view.pageIndex = i;
[pagesArray addObject:view];
}
}
- (RTPSinglePhotoViewController *)viewControllerAtIndex:(NSUInteger)index
{
NSLog(#"PHOTO ARRAY COUNT IS: %lu", (unsigned long)[photosArray count]);
if ([self.pagesArray count] == 0) {
return nil;
}
return [pagesArray objectAtIndex:index];
;
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = ((RTPSinglePhotoViewController*) viewController).pageIndex;
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
NSLog(#"THE INDEX NOW IS %lu",(unsigned long)index);
index--;
return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((RTPSinglePhotoViewController*) viewController).pageIndex;
if (index == ([pagesArray count]-1) || index == NSNotFound) {
return nil;
}
NSLog(#"THE INDEX NOW IS %lu",(unsigned long)index);
index++;
return [self viewControllerAtIndex:index];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.photosArray count];
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#end
RTPSinglePhotoViewController is the content view controller holding images here is the code:
#import "RTPSinglePhotoViewController.h"
#import "JMImageCache.h"
#interface RTPSinglePhotoViewController ()
#end
#implementation RTPSinglePhotoViewController
#synthesize imageView,imageUrl;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString * path = imageUrl;
NSURL * url = [NSURL URLWithString:path];
// [cell.thumbnailImageView setImageWithURL:url key:nil placeholder:nil completionBlock:nil failureBlock:nil];
[[JMImageCache sharedCache] imageForURL:url completionBlock:^(UIImage *downloadedImage) {
imageView.image = downloadedImage;
} failureBlock:nil];
// Do any additional setup after loading the view.
}
#end
Now, according to the log, the image is being downloaded/cached properly, it is just not showing up. Seeing a blank page instead of photo. What am I doing wrong?
Any suggestions would be appreciated!
I suppose you are not using storyboard.
The way you are allocating view controllers is:
RTPSinglePhotoViewController *view = [[RTPSinglePhotoViewController alloc] init] ;
Where it should be:
RTPSinglePhotoViewController *view = [[RTPSinglePhotoViewController alloc] initWithNibName:#"The name of the xib associated with this controller" bundle: [NSBundle mainBundle]];
The problem is that you are not associating any view to your view controller, therefore nothing is displayed
EDIT: Since you're using storyboards the right way to instantiate the view controller is:
RTPSinglePhotoViewController *view = (RTPSinglePhotoViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"your_controllers_storyboard_id"];

UIPageViewController doesn't change page

I have a view controller implementing UIPageViewControllerDataSource delegate, and it contains a UIPageViewController.
My issue is that, after 3 hours of tutorials and reading, I still don't understand why the UIPageController reacts to swipe by moving it's content, but it doesn't change the page if the scroll is enough. It's always stuck on the first page.
So this is my .h file
#import <UIKit/UIKit.h>
#interface BreathePageViewController : UIViewController <UIPageViewControllerDataSource>
#property (strong, nonatomic) UIPageViewController *pageController;
#end
And this is my .m file
#import "BreathePageViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h.h"
#interface BreathePageViewController () {
NSArray *pageViewControllerScreens;
FirstViewController *firstViewController;
SecondViewController *secondViewController;
int pageIndex;
}
#end
#implementation BreathePageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
pageIndex = 0;
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[[self.pageController view] setFrame:[[self view] bounds]];
[self.pageController setViewControllers:#[firstViewController] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self.view addSubview:self.pageController.view];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
if (pageIndex == 0) {
return nil;
}
pageIndex--;
return [self viewControllerAtIndex:pageIndex];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
if (pageIndex == 2) {
return nil;
}
pageIndex++;
return [self viewControllerAtIndex:pageIndex];
}
- (UIViewController *)viewControllerAtIndex:(NSUInteger)index {
UIViewController *vc;
if (pageIndex == 0 ) {
vc = [[FirstViewController alloc] init];
}
else if (pageIndex == 1) {
vc = [[SecondViewController alloc] init];
}
return vc;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController {
return 2;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
return 0;
}
#end
Now the strange stuff is that the pageViewController:viewControllerAfterViewController is never called.
Can someone help me out?
Thank you
After a night of sleep, I am going to to reply my own question.
I don't know why (any comment appreciated), but the problem was not on that view controller, but it was in the one I was creating it in.
In the parent view controller I was building the UIPageViewController like that:
BreathePageViewController *pageController = [[BreathePageViewController alloc] init];
[pageController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:pageController.view];
And now I tried to move the declaration of pageViewController as an iVar. And it works.
Hope to help someone else

UIPageviewController using Storyboard crash

I'm gonna make a tutorial page using UIPageViewController.
But crash happened. I couldn't find the point occurring crash.
I'm trying to use UIPageViwController directly, not using in UIViewController.
dataSource works. but when trying to call [self setViewControllers:#[pageZero] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
, an error occurred like the below.
UserGuidePageViewController is linked to UIPagveViewController Frame in starboard.
Following is Header
#import <UIKit/UIKit.h>
#interface UserGuidePageViewController : UIPageViewController <UIPageViewControllerDataSource> {
}
#end
Following is code
#import "UserGuidePageViewController.h"
#import "UserGuideViewController.h"
#interface UserGuidePageViewController () {
}
#property (nonatomic, retain) NSArray *array;
#end
#implementation UserGuidePageViewController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.dataSource = self;
}
return self;
}
- (UserGuideViewController *)userGuideViewForPageIndex:(NSUInteger)pageIndex
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UserGuideViewController *targetViewController = [storyboard instantiateViewControllerWithIdentifier:#"UserGuideViewController"];
return targetViewController;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UserGuideViewController *pageZero = [self userGuideViewForPageIndex:0];
[self setViewControllers:#[pageZero] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
--> Crash happened here!!!
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Page View Controller Data Source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [(UserGuideViewController *)viewController pageIndex];
return [self userGuideViewForPageIndex:(index - 1)];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [(UserGuideViewController *)viewController pageIndex];
return [self userGuideViewForPageIndex:(index + 1)];
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
// The number of items reflected in the page indicator.
return 1;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
// The selected item reflected in the page indicator.
return 0;
}
Error is following.
*** Assertion failure in -[_UIQueuingScrollView setView:direction:animated:completion:], /SourceCache/UIKit/UIKit-2903.23/_UIQueuingScrollView.m:671
2013-12-18 15:23:35.779 Natural Calendar[4497:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: view'
*** First throw call stack:
(0x30ef6f4b 0x3b6d36af 0x30ef6e25 0x3189efe3 0x33ba3321 0x33b382eb 0x33b38407 0xf4715 0x3366e37b 0x3366e139 0xf0c45 0x1062d9 0x1106c3 0x3369e713 0x3369e6b3 0x3369e691 0x3368a11f 0x3369e107 0x3369ddd9 0x33698e65 0x3366e79d 0x3366cfa3 0x30ec2183 0x30ec1653 0x30ebfe47 0x30e2ac27 0x30e2aa0b 0x35b0b283 0x336ce049 0xecd09 0x3bbdbab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Actually, I changed into Xcode template style.
But this error is also occurred.
- (void)viewDidLoad
{
[super viewDidLoad];
_pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
[_pageViewController setDataSource:self];
UserGuideViewController *pageZero = [self userGuideViewForPageIndex:0];
[_pageViewController setViewControllers:#[pageZero] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
[self addChildViewController:_pageViewController];
[self.view addSubview:[_pageViewController view]];
CGRect pageViewRect = self.view.bounds;
//pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0);
_pageViewController.view.frame = pageViewRect;
[_pageViewController didMoveToParentViewController:self];
}
I don't know what's problem. When choosing UIPageViewControllerTransitionStyleScroll, the error occurred. When choosing UIPageViewControllerTransitionStylePageCurl, the error is not happened.
#interface UserGuidePageViewController : UIPageViewController <UIPageViewControllerDataSource> {
chnge this line with
#interface UserGuidePageViewController : UIViewController <UIPageViewControllerDataSource> {
Means you can use UIViewcontroller for this
see this link for reference.
and this another link which is used storyboard.
I found loadView() { } declared at page ContentViewController. However loadView() remains empty without calling [super loadView];. As a result from this, the view would remain nil.
Now it appears to work after the removal of the method, loadView().

How to implement UIPageViewController with a UIScrollView?

I took the Photo Scroller example from Apple's website and tried to implement my own Album by copying the code. Now, the UIScrollView is not visible. How do I make it appear?
The only code change that I made was in creating the UIPageViewController. In my case, it's a UIViewController that is opening it and not the AppDelegate.
#implementation BasePhotoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nil bundle:nibBundleOrNil];
if (self) {
// kick things off by making the first page
PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
if (pageZero != nil)
{
// assign the first page to the pageViewController (our rootViewController)
//UIPageViewController *pageViewController = (UIPageViewController *) [[UIApplication sharedApplication] keyWindow].rootViewController;
UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0 navigationOrientation:0 options:nil];
//UIPageViewController *pageViewController = (UIPageViewController *)self.parentViewController;
pageViewController.dataSource = self;
[pageViewController setViewControllers:#[pageZero]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
}
}
return self;
}
You are not adding the pageViewController's view as a subview of BasePhotoViewController's view. Your BasePhotoViewController class should look something like this. Note the code in viewDidLoad.
BasePhotoViewController.h:
#interface BasePhotoViewController : UIViewController <UIPageViewControllerDataSource>
#property (nonatomic, strong) UIPageViewController * pageViewController;
#end
BasePhotoViewController.m:
#import "BasePhotoViewController.h"
#import "PhotoViewController.h"
#implementation BasePhotoViewController
#synthesize pageViewController;
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
if (pageZero != nil)
{
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0
navigationOrientation:0
options:nil];
self.pageViewController.dataSource = self;
[self.pageViewController setViewControllers:#[pageZero]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:NULL];
}
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];
}
# pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerBeforeViewController:(PhotoViewController *)vc
{
NSUInteger index = vc.pageIndex;
return [PhotoViewController photoViewControllerForPageIndex:(index - 1)];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerAfterViewController:(PhotoViewController *)vc
{
NSUInteger index = vc.pageIndex;
return [PhotoViewController photoViewControllerForPageIndex:(index + 1)];
}
#end
Note: I've initialised the UIPageViewController in initWithCoder: because Apple's Photo Scroller code uses a storyboard. I removed the UIPageViewController from the storyboard and created a BasePhotoViewController in its place. If you are not loading BasePhotoViewController from a storyboard, you should move the code in initWithCoder: to the appropriate initialiser.
EDIT: See this sample project on github.

Resources