Skip to content

Instantly share code, notes, and snippets.

@justincbeck
Created November 12, 2012 20:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justincbeck/4061693 to your computer and use it in GitHub Desktop.
Save justincbeck/4061693 to your computer and use it in GitHub Desktop.
ContainerViewController
//============ 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]];
}
@RandeepiPhone125
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment