public
Last active

ContainerViewController

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
//============ AppDelegate ===============
 
@implementation AppDelegate
 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
NSMutableArray *controllers = [NSMutableArray array];
ViewController *redController = [[ViewController alloc] initWithNibName:nil bundle:nil];
redController.view.backgroundColor = [UIColor redColor];
[controllers addObject:redController];
ViewController *greenController = [[ViewController alloc] initWithNibName:nil bundle:nil];
greenController.view.backgroundColor = [UIColor greenColor];
[controllers addObject:greenController];
ViewController * blueController = [[ViewController alloc] initWithNibName:nil bundle:nil];
blueController.view.backgroundColor = [UIColor blueColor];
[controllers addObject:blueController];
ViewController * purpleController = [[ViewController alloc] initWithNibName:nil bundle:nil];
purpleController.view.backgroundColor = [UIColor purpleColor];
[controllers addObject:purpleController];
ViewController * blackController = [[ViewController alloc] initWithNibName:nil bundle:nil];
blackController.view.backgroundColor = [UIColor blackColor];
[controllers addObject:blackController];
ViewController * whiteController = [[ViewController alloc] initWithNibName:nil bundle:nil];
whiteController.view.backgroundColor = [UIColor whiteColor];
[controllers addObject:whiteController];
ContainerViewController *viewController = [[ContainerViewController alloc] initWithViewControllers: controllers];
viewController.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
 
//============ ContainerViewController ==============
 
@interface ContainerViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
 
@property (nonatomic, strong) NSMutableArray *controllers;
@property (nonatomic, strong) UIPageViewController *pageViewController;
 
@end
 
@implementation ContainerViewController
 
- (id)initWithViewControllers:(NSMutableArray *)controllers
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.controllers = controllers;
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:28.0f] forKey:UIPageViewControllerOptionInterPageSpacingKey]];
self.pageViewController.view.frame = CGRectMake(0.0f, 0.0f, 320.0f, 568.0f);
self.pageViewController.delegate = self;
self.pageViewController.dataSource = self;
[self.pageViewController setViewControllers:[NSArray arrayWithObject:[controllers objectAtIndex:0] ] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
// Nothing to see here
}];
[self addChildViewController:self.pageViewController];
[self.view addSubview:self.pageViewController.view];
}
return self;
}
 
#pragma mark UIPageViewControllerDelegate
 
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
// Nothing to see here
}
 
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
// Nothing to see here
}
 
#pragma mark UIPageViewControllerDataSource
 
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
int index = [self.controllers indexOfObject:viewController];
if (index - 1 >= 0)
{
return [self.controllers objectAtIndex:index - 1];
}
return nil;
}
 
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
int index = [self.controllers indexOfObject:viewController];
if (index + 1 < [self.controllers count])
{
return [self.controllers objectAtIndex:index + 1];
}
return nil;
}
 
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return [self.controllers count];
}
 
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return [self.controllers indexOfObject:[pageViewController.viewControllers objectAtIndex:0]];
}

This is the most simple code i have found on net. And it is easiest to understand. Thanks Justin.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.