Created
September 15, 2015 06:01
-
-
Save abbeyjackson/1a4a1cb60a0cd73760bc to your computer and use it in GitHub Desktop.
This is the code for the Container View Controller when setting up a UIContainerView
From http://sandmoose.com/post/35714028270/storyboards-with-custom-container-view-controllers
This file contains hidden or 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 "ContainerViewController.h" | |
#define SegueIdentifierFirst @"embedFirst" | |
#define SegueIdentifierSecond @"embedSecond" | |
@interface ContainerViewController () | |
@property (strong, nonatomic) NSString *currentSegueIdentifier; | |
@end | |
@implementation ContainerViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
self.currentSegueIdentifier = SegueIdentifierFirst; | |
[self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil]; | |
} | |
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
{ | |
if ([segue.identifier isEqualToString:SegueIdentifierFirst]) | |
{ | |
if (self.childViewControllers.count > 0) { | |
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController]; | |
} | |
else { | |
[self addChildViewController:segue.destinationViewController]; | |
((UIViewController *)segue.destinationViewController).view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); | |
[self.view addSubview:((UIViewController *)segue.destinationViewController).view]; | |
[segue.destinationViewController didMoveToParentViewController:self]; | |
} | |
} | |
else if ([segue.identifier isEqualToString:SegueIdentifierSecond]) | |
{ | |
[self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController]; | |
} | |
} | |
- (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController | |
{ | |
toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); | |
[fromViewController willMoveToParentViewController:nil]; | |
[self addChildViewController:toViewController]; | |
[self transitionFromViewController:fromViewController toViewController:toViewController duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { | |
[fromViewController removeFromParentViewController]; | |
[toViewController didMoveToParentViewController:self]; | |
}]; | |
} | |
- (void)swapViewControllers | |
{ | |
self.currentSegueIdentifier = (self.currentSegueIdentifier == SegueIdentifierFirst) ? SegueIdentifierSecond : SegueIdentifierFirst; | |
[self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment