ioscocoa-touchsubviewuicontainerviewchildviewcontroller

How to add a Container View programmatically


A Container View can be easily added into a storyboard through Interface Editor. When added, a Container View is of a placeholder view, an embed segue, and a (child) view controller.

However, I am not able to find a way to add a Container View programmatically. Actually, I am not even able to find a class named UIContainerView or so.

A name for the class of Container View is surely a good start. A complete guide including the segue will be much appreciated.

I am aware of View Controller Programming Guide, but I do not regard it as the same as the way Interface Builder does for Container Viewer. For example, when the constraints are properly set, the (child) view will adapts to the size changes in Container View.


Solution

  • A storyboard "container view" is just a standard UIView object. There is no special "container view" type. In fact, if you look at the view hierarchy, you can see that the "container view" is a standard UIView:

    container view

    To achieve this programmatically, you employ "view controller containment":

    See Implementing a Container View Controller in the View Controller Programming Guide and the "Implementing a Container View Controller" section of the UIViewController Class Reference.


    For example, in Swift 4.2 it might look like:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let controller = storyboard!.instantiateViewController(withIdentifier: "Second")
        addChild(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(controller.view)
    
        NSLayoutConstraint.activate([
            controller.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
            controller.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
            controller.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
            controller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10)
        ])
    
        controller.didMove(toParent: self)
    }
    

    Note, the above doesn't actually add a "container view" to the hierarchy. If you want to do that, you'd do something like:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // add container
    
        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(containerView)
        NSLayoutConstraint.activate([
            containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
            containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
            containerView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
            containerView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -10),
        ])
    
        // add child view controller view to container
    
        let controller = storyboard!.instantiateViewController(withIdentifier: "Second")
        addChild(controller)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(controller.view)
    
        NSLayoutConstraint.activate([
            controller.view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            controller.view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            controller.view.topAnchor.constraint(equalTo: containerView.topAnchor),
            controller.view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
        ])
    
        controller.didMove(toParent: self)
    }
    

    This latter pattern is extremely useful if ever transitioning between different child view controllers and you just want to make sure one child's view is in the same location and the previous child's view (i.e. all the unique constraints for the placement are dictated by the container view, rather than needing to rebuild these constraints each time). But if just performing simple view containment, the need for this separate container view is less compelling.


    In the examples above, I’m setting translatesAutosizingMaskIntoConstraints to false defining the constraints myself. You obviously can leave translatesAutosizingMaskIntoConstraints as true and set both the frame and the autosizingMask for the views you add, if you’d prefer.


    See previous revisions of this answer for Swift 3 and Swift 2 renditions.