UIPageViewController as application's root controller
-
27-10-2019 - |
Question
I'm using UIPageViewController
for the first time. It's of course, iOS5 and I'm comfortable with ARC. I'm not using Storyboards.
In all the piecemeal examples I see, the UIPageViewController
never "owns" the screen. It is always the child of some other view controller. Is this a requirement? I want to make it the root controller of my app, much like is commonly done with UINavigationController
. I'm trying to set it all up programmatically, but all I get is an empty page view controller, with no content in it.
Here's how the app starts off (all code here is in my app delegate class):
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self configureLoggers];
[self applyAppearanceProxySettings];
[self configureDataStore];
[self configureNavigationController];
[self configurePageController]; // <-- this is where we do the page view controller setup, shown next.
self.window.rootViewController = self.pageController;
[self.window makeKeyAndVisible];
log4Info(@"Application completed launching.");
return YES;
}
And that method mentioned in the code above, that does the setup for the Page View Controller:
- (void)configurePageController {
CCFrontCoverViewController *frontCoverViewController = [[CCFrontCoverViewController alloc] initWithNibName:nil bundle:nil];
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:UIPageViewControllerSpineLocationMin]
forKey:UIPageViewControllerOptionSpineLocationKey];
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:options];
self.pageController.delegate = self;
self.pageController.dataSource = self;
self.pageController.view.backgroundColor = [UIColor redColor]; // So we know we're missing content because red shows through.
NSArray *pageViewControllers = [NSArray arrayWithObjects:frontCoverViewController, self.navigationController, nil];
[self.pageController setViewControllers:pageViewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
I've also put logging in the two data source callback methods, and they don't even get called.
When this is run, I just get a blank red screen. Again, there's no storyboard involved. What am I missing?
Solution
I am able to use a UIPageViewController
as the root view controller in a sample project provided that:
- The
UIPageViewController
is initialized properly with a valid spine location. - The
UIPageViewController
'sdoubleSided
property is set (consistent with the spine location). - The
UIPageViewController
'sviewControllers
array is set (again, consistent with the spine location anddoubleSided
property).
It looks like you haven't set doubleSided
to YES
in your code (since your viewControllers
array has two items in it). Does adding that before you set the viewControllers
array fix your problem?
EDIT: Just for posterity, in the UIPageViewController docs there is a chart in the description of the setViewControllers:direction:animation:completion:
method showing the number of view controllers allowed for all valid combinations of spine location and doubleSided
.