iosswiftuicontainerview

Can I create a UIContainerView programmatically?


I'm trying to create a dynamic view flow that uses UIContainerViews to host UIViewControllers. The UIContainerViews (or UIViewControllers) need to be programmatically added to the UIView to allow multiple side by side.

I've been looking around, but can't find any constructors I can use to create my UIContainerView.

Is there a simple way to do this or is it against normal guidelines for creating reusable views?

To sum up, I want to achieve something like this:

var containerView = UIContainerView()
containerView.add(myViewController)

Solution

  • A UIContainerView is just a visual way to add a viewController as a child of another and setting its bounds.

    You can do this programatically by adding the second viewController as a child of the first, taking the second's view and placing it somewhere on the first's view.

    Something like this;

    childVC = [[SomeViewController alloc] init];
    
    [self addChildViewController:childVC];
    
    [self.view addSubview:childVC.view];
    
    [childVC didMoveToParentViewController:self];