Last active
December 25, 2015 17:39
-
-
Save jonfriskics/7014535 to your computer and use it in GitHub Desktop.
UIPageViewController example - demo of the pageViewController not interfering with touches on the child VCs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "AppDelegate.h" | |
#import "ContainerVC.h" | |
@implementation AppDelegate | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
self.window.rootViewController = [[ContainerVC alloc] init]; | |
self.window.tintColor = [UIColor whiteColor]; | |
self.window.backgroundColor = [UIColor whiteColor]; | |
[self.window makeKeyAndVisible]; | |
return YES; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface ContainerVC : UIViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate> | |
@property (strong, nonatomic) UIPageViewController *pageViewController; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ContainerVC.h" | |
#import "Page1VC.h" | |
#import "Page2VC.h" | |
@interface ContainerVC () | |
@property (strong, nonatomic) NSArray *dataSource; | |
@end | |
@implementation ContainerVC | |
- (void)loadView | |
{ | |
UIView *view = [[UIView alloc] init]; | |
view.backgroundColor = [UIColor blackColor]; | |
self.view = view; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll | |
navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal | |
options:nil]; | |
self.pageViewController.dataSource = self; | |
self.pageViewController.delegate = self; | |
[[self.pageViewController view] setFrame:CGRectMake(0, 0, 320, 500)]; | |
Page1VC *page1VC = [[Page1VC alloc] init]; | |
page1VC.index = 0; | |
Page2VC *page2VC = [[Page2VC alloc] init]; | |
page2VC.index = 1; | |
self.dataSource = @[page1VC, page2VC]; | |
[self.pageViewController setViewControllers:@[self.dataSource[0]] | |
direction:UIPageViewControllerNavigationDirectionForward | |
animated:YES | |
completion:nil]; | |
[self addChildViewController:self.pageViewController]; | |
[self.view addSubview:self.pageViewController.view]; | |
[self.pageViewController didMoveToParentViewController:self]; | |
} | |
- (void)viewWillLayoutSubviews | |
{ | |
[super viewWillLayoutSubviews]; | |
self.pageViewController.view.frame = CGRectMake(0, 0, 320, CGRectGetHeight(self.view.frame)); | |
} | |
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController | |
viewControllerBeforeViewController:(UIViewController *)viewController | |
{ | |
NSUInteger index = [(id)viewController index]; | |
if(index == 0) { | |
return nil; | |
} | |
index--; | |
return self.dataSource[index]; | |
} | |
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController | |
viewControllerAfterViewController:(UIViewController *)viewController | |
{ | |
NSUInteger index = [(id)viewController index]; | |
index++; | |
if(index == 2) { | |
return nil; | |
} | |
return self.dataSource[index]; | |
} | |
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController { | |
return self.dataSource.count; | |
} | |
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController { | |
return 0; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface Page1VC : UIViewController | |
@property (assign, nonatomic) NSInteger index; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Page1VC.h" | |
@interface Page1VC () | |
@property (strong, nonatomic) UIButton *button; | |
@end | |
@implementation Page1VC | |
- (void)loadView | |
{ | |
UIView *view = [[UIView alloc] init]; | |
view.backgroundColor = [UIColor colorWithRed:1.0 green:0.3 blue:0.3 alpha:1.0]; | |
self.view = view; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
if(![self.button isDescendantOfView:self.view]) { | |
self.button = [UIButton buttonWithType:UIButtonTypeSystem]; | |
[self.button setTitle:@"click me" forState:UIControlStateNormal]; | |
[self.button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside]; | |
self.button.frame = CGRectMake(200, 200, 80, 30); | |
[self.view addSubview:self.button]; | |
} | |
} | |
- (void)clicked:(id)sender | |
{ | |
NSLog(@"button click received from Page1VC"); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface Page2VC : UIViewController | |
@property (assign, nonatomic) NSInteger index; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Page2VC.h" | |
@interface Page2VC () | |
@property (strong, nonatomic) UIButton *button; | |
@end | |
@implementation Page2VC | |
- (void)loadView | |
{ | |
UIView *view = [[UIView alloc] init]; | |
view.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:1.0 alpha:1.0]; | |
self.view = view; | |
} | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
[super viewWillAppear:animated]; | |
if(![self.button isDescendantOfView:self.view]) { | |
self.button = [UIButton buttonWithType:UIButtonTypeSystem]; | |
[self.button setTitle:@"click me" forState:UIControlStateNormal]; | |
[self.button addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside]; | |
self.button.frame = CGRectMake(200, 120, 80, 30); | |
[self.view addSubview:self.button]; | |
} | |
} | |
- (void)clicked:(id)sender | |
{ | |
NSLog(@"button click received from Page2VC"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment