Skip to content

Add child view controller utilities #137

@ZevEisenberg

Description

@ZevEisenberg

These have been used in an internal project. I think they would be a good addition to RZUtils. I'll throw the code here, and make a PR at some point.

For discussion: Do we like the dependence on the RZ auto layout helpers, or should this do the layout independently?

@interface UIViewController (RZViewControllerContainment)

/**
 *  Adds a view controller as a child of the receiver, and uses Auto Layout to fill the receiver.
 *
 *  @param childViewController The view controller to add as a child.
 */
- (void)rz_addChildViewController:(UIViewController *)childViewController;

/**
 *  Adds a view controller as a child of the receiver, and uses Auto Layout to fill a specified subview of the receiver.
 *
 *  @param childViewController The view controller to add as a child.
 *  @param view                The view to fill with the child view controller’s view. Must be a subview of the receiver’s view.
 */
- (void)rz_addChildViewController:(UIViewController *)childViewController toView:(UIView *)view;

/**
 *  Adds a view controller as a child of the receiver. Provides a way to set your own constraints to define how the child view controller’s view fills the parent view.
 *
 *  @param childViewController The view controller to add as a child.
 *  @param view                The view to act as the superview to the child view controller’s view. Must be a subview of the receiver’s view.
 *  @param constraintBlock     This block will be called synchronously when the child view controller’s view is ready to receive constraints. Use it to configure custom constraints on the child view controller. If you need to access the parent view, use the child view controller’s @c superview property.
 */
- (void)rz_addChildViewController:(UIViewController *)childViewController toView:(UIView *)view constraintBlock:(void(^)(UIView *childViewControllerView))constraintBlock;

/**
 *  Removes a view controller as a child of the receiver.
 *
 *  @param childViewController The child view controller to remove.
 */
- (void)rz_removeChildViewController:(UIViewController *)childViewController;

@end
@implementation UIViewController (RZViewControllerContainment)

- (void)rz_addChildViewController:(UIViewController *)childViewController
{
    [self rz_addChildViewController:childViewController toView:self.view];
}

- (void)rz_addChildViewController:(UIViewController *)childViewController toView:(UIView *)view
{
    [self rz_addChildViewController:childViewController toView:view constraintBlock:^(UIView *childViewControllerView) {
        [childViewControllerView rz_fillContainerWithInsets:UIEdgeInsetsZero];
    }];
}

- (void)rz_addChildViewController:(UIViewController *)childViewController toView:(UIView *)view constraintBlock:(void(^)(UIView *childViewControllerView))constraintBlock
{
    RZASSERT_NOT_NIL(constraintBlock);
    RZASSERT_TRUE_WITH_MESSAGE([view isDescendantOfView:self.view], @"view must be self.view or a descendant of self.view");

    [self addChildViewController:childViewController];
    childViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
    [view addSubview:childViewController.view];
    if ( constraintBlock ) {
        constraintBlock(childViewController.view);
    }
    [childViewController didMoveToParentViewController:self];
}

- (void)rz_removeChildViewController:(UIViewController *)childViewController
{
    if ( [self.childViewControllers containsObject:childViewController] ) {
        [childViewController willMoveToParentViewController:nil];
        [childViewController.view removeFromSuperview];
        [childViewController removeFromParentViewController];
    }
}

@end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions