forked from peterpaulis/UIViewController--Container-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIViewController+Container.m
executable file
·75 lines (59 loc) · 3.03 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
65
66
67
68
69
70
71
72
73
74
75
//
// 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 toContainerView:(UIView *)view resizeToContainer:(BOOL)resizeToContainer useAutolayout:(BOOL)autolayout {
[self addChildViewController:childViewController];
if (resizeToContainer){
childViewController.view.frame = view.bounds;
childViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}
[view addSubview:childViewController.view];
[childViewController didMoveToParentViewController:self];
[view bringSubviewToFront:childViewController.view];
if (resizeToContainer && 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 resizeToContainer:(BOOL)resizeToContainer{
[self containerAddChildViewController:childViewController
toContainerView:view
resizeToContainer:resizeToContainer
useAutolayout:NO];
}
- (void)containerAddChildViewController:(UIViewController *)childViewController toContainerView:(UIView *)view {
[self containerAddChildViewController:childViewController
toContainerView:view
resizeToContainer:NO
useAutolayout:NO];
}
- (void)containerAddChildViewController:(UIViewController *)childViewController {
[self containerAddChildViewController:childViewController toContainerView:self.view];
}
- (void)containerRemoveChildViewController:(UIViewController *)childViewController {
[childViewController willMoveToParentViewController:nil];
[childViewController.view removeFromSuperview];
[childViewController removeFromParentViewController];
}
- (void)containerRemoveFromParentViewController {
[self willMoveToParentViewController:nil];
[self.view removeFromSuperview];
[self removeFromParentViewController];
}
@end