× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Implementing UIPageViewController

<p><span style="font-size:14px">I have seen some people on the internet sharing code snippets on how to implement the UIPageViewController for Welcome screens and other uses. Most of these impemetations use a UIViewController as a container for the UIPageViewController. Personally, I don't like the idea of abusing the use of UIViewControllers. The UIPageViewController is already a ViewController, which means that comes with a view and is presentable by other ViewControllers.</span></p><p><span style="font-size:14px">Below is my own custom implementation of a custom class "WelcomeTutorialHelper", which is a NSObject that serves both as the DataSource and Delegate for the UIPageViewController.</span></p><p><span style="font-size:14px">In this case, a particular class creates and keeps an instance of the WelcomeTutorialHelper, and calls the createWelcomeController to obtain a UIPageViewController already configured and ready to be presented.</span></p><p><span style="font-size:14px">Note that background and view configuration is done directly via de UIPageViewController, and not through a "middle-man".</span></p><p><span style="font-size:14px">Here's the code:</span></p><pre><code class="language-objectivec">@implementation WelcomeTutorialHelper { WelcomeViewController *welcomeViewController; BeforeViewController *beforeViewController; AfterViewController *afterViewController; } - (id)init{ self = [super init]; if (self) { } return self; } - (UIPageViewController *)createWelcomeController{ UIPageViewController *pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; [pageController.view setBackgroundColor:[UIColor whiteColor]]; pageController.dataSource = self; pageController.delegate = self; CGRect frame = [UIApplication sharedApplication].keyWindow.bounds; [pageController.view setFrame:frame]; welcomeViewController = [((UIStoryboard *)[UIStoryboard storyboardWithName:@"Welcome" bundle:nil]) instantiateViewControllerWithIdentifier:@"WelcomeViewController"]; beforeViewController = [((UIStoryboard *)[UIStoryboard storyboardWithName:@"Welcome" bundle:nil]) instantiateViewControllerWithIdentifier:@"BeforeViewController"]; afterViewController = [((UIStoryboard *)[UIStoryboard storyboardWithName:@"Welcome" bundle:nil]) instantiateViewControllerWithIdentifier:@"AfterViewController"]; [pageController setViewControllers:@[welcomeViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; [UIPageControl appearance].pageIndicatorTintColor = [UIColor grayColor]; [UIPageControl appearance].currentPageIndicatorTintColor = [UIColor galerieDarkGrayColor]; [UIPageControl appearance].backgroundColor = [UIColor whiteColor]; return pageController; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{ if ([viewController isKindOfClass:[WelcomeViewController class]]) { return nil; }else if ([viewController isKindOfClass:[BeforeViewController class]]){ return welcomeViewController; }else{ return beforeViewController; } } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{ if ([viewController isKindOfClass:[AfterViewController class]]) { return nil; }else if ([viewController isKindOfClass:[BeforeViewController class]]){ return afterViewController; }else{ return beforeViewController; } } - (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController{ return 3; } - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController{ return 0; } @end</code></pre><p> </p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus