Skip to content

Commit

Permalink
Autolayout fitting support
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpaulis committed Mar 28, 2014
1 parent 73dd50d commit 43219a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 5 additions & 1 deletion UIViewController+Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

@interface UIViewController (Container)

- (void)containerAddChildViewController:(UIViewController *)childViewController parentView:(UIView *)view;
- (void)containerAddChildViewController:(UIViewController *)childViewController parentView:(UIView *)view __attribute__((deprecated(use containerAddChildViewController:toContainerView:)));

- (void)containerAddChildViewController:(UIViewController *)childViewController toContainerView:(UIView *)view useAutolayout:(BOOL)autolayout;

- (void)containerAddChildViewController:(UIViewController *)childViewController toContainerView:(UIView *)view;

- (void)containerAddChildViewController:(UIViewController *)childViewController;

Expand Down
31 changes: 30 additions & 1 deletion UIViewController+Container.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,45 @@
@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 parentView:self.view];
[self containerAddChildViewController:childViewController toContainerView:self.view];

}

Expand Down

0 comments on commit 43219a3

Please sign in to comment.