-
Notifications
You must be signed in to change notification settings - Fork 9
/
UIViewController+Container.m
64 lines (45 loc) · 2.58 KB
/
UIViewController+Container.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// UIViewController+Container.m
// UIViewController+Container
//
// Created by Peter Paulis on 20.4.2013.
// Copyright (c) 2013 Peter Paulis. All rights reserved.
// min:60 - Building perfect apps - https://min60.com
#import "UIViewController+Container.h"
@implementation UIViewController (Container)
- (void)containerAddChildViewController:(UIViewController *)childViewController parentView:(UIView *)view {
[self containerAddChildViewController:childViewController toContainerView:view];
}
- (void)containerAddChildViewController:(UIViewController *)childViewController toContainerView:(UIView *)view useAutolayout:(BOOL)autolayout {
childViewController.view.frame = CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height);
[self addChildViewController:childViewController];
[view addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];
[view bringSubviewToFront:childViewController.view];
if (autolayout) {
UIView * parent = view;
UIView * child = childViewController.view;
[child setTranslatesAutoresizingMaskIntoConstraints:NO];
[parent addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[child]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(child)]];
[parent addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[child]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(child)]];
[parent layoutIfNeeded];
}
}
- (void)containerAddChildViewController:(UIViewController *)childViewController toContainerView:(UIView *)view {
[self containerAddChildViewController:childViewController toContainerView:view useAutolayout:NO];
}
- (void)containerAddChildViewController:(UIViewController *)childViewController {
[self containerAddChildViewController:childViewController toContainerView:self.view];
}
- (void)containerRemoveChildViewController:(UIViewController *)childViewController {
[childViewController willMoveToParentViewController:nil];
[childViewController.view removeFromSuperview];
[childViewController removeFromParentViewController];
}
@end