Skip to content

Commit 2b056a5

Browse files
committed
Initial commit.
1 parent 7913801 commit 2b056a5

30 files changed

+1631
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.lock
2+
.DS_Store
3+
xcuserdata
4+
project.xcworkspace
5+
Build

GRKContainerViewController.podspec

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Pod::Spec.new do |s|
2+
s.name = "GRKContainerViewController"
3+
s.version = "1.0"
4+
s.summary = "A simple container view controller used to easily swap contained controllers with optional animation."
5+
s.description = <<-DESC
6+
A container UIViewController providing the ability to easily transition from one contained view controller to another.
7+
DESC
8+
s.homepage = "https://github.com/levigroker/GRKContainerViewController"
9+
s.license = 'Creative Commons Attribution 3.0 Unported License'
10+
s.author = { "Levi Brown" => "[email protected]" }
11+
s.social_media_url = 'https://twitter.com/levigroker'
12+
s.source = { :git => "https://github.com/levigroker/GRKContainerViewController.git", :tag => "1.0" }
13+
s.platform = :ios, '7.1'
14+
s.ios.deployment_target = '6.0'
15+
s.source_files = 'GRKContainerViewController/**/*.{h,m}'
16+
s.requires_arc = true
17+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 <UIKit/UIKit.h>
19+
20+
/**
21+
* The default animation duration for the transition to the new view controller.
22+
*/
23+
extern NSTimeInterval const kDefaultAnimationDuration;
24+
25+
///
26+
/// @name GRKContainerViewController
27+
///
28+
29+
@interface GRKContainerViewController : UIViewController
30+
31+
/**
32+
* The current view controller. Changes to this property will not be animated.
33+
*/
34+
@property (nonatomic,strong) UIViewController *viewController;
35+
36+
/**
37+
* The animation duration to use for the transition animation (if applicable). Defaults to `kDefaultAnimationDuration`.
38+
* @see kDefaultAnimationDuration
39+
*/
40+
@property (nonatomic,assign) NSTimeInterval transitionAnimationDuration;
41+
42+
/**
43+
* Set the currently displayed view controller with an optional cross fade animation and completion handler.
44+
*
45+
* @param viewController The new view controler to be displayed.
46+
* @param animated If YES then a simple cross fade animation will be applied during the tranistion from the current view controller to the new view controller.
47+
* @param completion A block to be called once the new view controller is displayed (after any animations). This can be `nil`.
48+
*/
49+
- (void)setViewController:(UIViewController *)viewController animated:(BOOL)animated completion:(void(^)(UIViewController *viewController))completion;
50+
51+
@end
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

Comments
 (0)