|
| 1 | +// |
| 2 | +// GRKContainerViewController.h |
| 3 | +// |
| 4 | +// Created by Levi Brown on November 23, 2013. |
| 5 | +// Copyright (c) 2013, 2014 Levi Brown <mailto:[email protected]> |
| 6 | +// This work is licensed under the Creative Commons Attribution 3.0 |
| 7 | +// Unported License. To view a copy of this license, visit |
| 8 | +// http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative |
| 9 | +// Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, |
| 10 | +// USA. |
| 11 | +// |
| 12 | +// The above attribution and the included license must accompany any version |
| 13 | +// of the source code. Visible attribution in any binary distributable |
| 14 | +// including this work (or derivatives) is not required, but would be |
| 15 | +// appreciated. |
| 16 | +// |
| 17 | + |
| 18 | +#import "GRKContainerViewController.h" |
| 19 | + |
| 20 | +NSTimeInterval const kDefaultAnimationDuration = 0.5f; |
| 21 | + |
| 22 | +@implementation GRKContainerViewController |
| 23 | + |
| 24 | +#pragma mark - Lifecycle |
| 25 | + |
| 26 | +- (instancetype)init |
| 27 | +{ |
| 28 | + if ((self = [super init])) |
| 29 | + { |
| 30 | + [self setup]; |
| 31 | + } |
| 32 | + |
| 33 | + return self; |
| 34 | +} |
| 35 | + |
| 36 | +- (instancetype)initWithCoder:(NSCoder *)aDecoder |
| 37 | +{ |
| 38 | + if ((self = [super initWithCoder:aDecoder])) |
| 39 | + { |
| 40 | + [self setup]; |
| 41 | + } |
| 42 | + |
| 43 | + return self; |
| 44 | +} |
| 45 | + |
| 46 | +- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil |
| 47 | +{ |
| 48 | + if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) |
| 49 | + { |
| 50 | + [self setup]; |
| 51 | + } |
| 52 | + |
| 53 | + return self; |
| 54 | +} |
| 55 | + |
| 56 | +- (void)setup |
| 57 | +{ |
| 58 | + _transitionAnimationDuration = kDefaultAnimationDuration; |
| 59 | +} |
| 60 | + |
| 61 | +#pragma mark - API Implementation |
| 62 | + |
| 63 | +- (void)setViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void(^)(UIViewController *viewController))completion |
| 64 | +{ |
| 65 | + //Ensure the view is loaded |
| 66 | + [self view]; |
| 67 | + |
| 68 | + //We will use a simple cross fade animation, as indicated |
| 69 | + NSTimeInterval duration = animated ? self.transitionAnimationDuration : 0.0f; |
| 70 | + |
| 71 | + //Set the new view controller's view to be transparent |
| 72 | + viewController.view.alpha = 0.0f; |
| 73 | + |
| 74 | + //Add the new view controller to the view and view controller hierarchies |
| 75 | + if (viewController) |
| 76 | + { |
| 77 | + [self addChildViewController:viewController]; |
| 78 | + [self.view addSubview:viewController.view]; |
| 79 | + [viewController didMoveToParentViewController:self]; |
| 80 | + |
| 81 | + //Setup constraints to keep the new view pinned to our size. |
| 82 | + UIView *containedView = viewController.view; |
| 83 | + containedView.translatesAutoresizingMaskIntoConstraints = NO; |
| 84 | + [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[containedView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containedView)]]; |
| 85 | + [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[containedView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containedView)]]; |
| 86 | + } |
| 87 | + |
| 88 | + [UIView animateWithDuration:duration animations:^{ |
| 89 | + //Set the new view controller's view to be visible |
| 90 | + viewController.view.alpha = 1.0f; |
| 91 | + } completion:^(BOOL finished) { |
| 92 | + //Remove the previous view controller from the view and view controller hierarchies |
| 93 | + [self.viewController willMoveToParentViewController:nil]; |
| 94 | + [self.viewController.view removeFromSuperview]; |
| 95 | + [self.viewController removeFromParentViewController]; |
| 96 | + |
| 97 | + //Keep our new view controller |
| 98 | + _viewController = viewController; |
| 99 | + |
| 100 | + if (completion) |
| 101 | + { |
| 102 | + //Call the completion block with the new view controller |
| 103 | + completion(self.viewController); |
| 104 | + } |
| 105 | + }]; |
| 106 | +} |
| 107 | + |
| 108 | +#pragma mark - Accessors |
| 109 | + |
| 110 | +- (void)setViewController:(UIViewController *)viewController |
| 111 | +{ |
| 112 | + [self setViewController:viewController animated:NO completion:nil]; |
| 113 | +} |
| 114 | + |
| 115 | +@end |
0 commit comments