Loading a ViewController inside a Container View

Solution 1:

You need to tell your BannerContainer view controller that it has a new child controller, and to tell the Child that it has a parent VC. This is described in the Apple Docs here. Like this:

   [self addChildViewController:vc];
   vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
   [self.container addSubview:vc.view];
   [vc didMoveToParentViewController:self];

Or in Swift:

    self.addChildViewController(vc)
    vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height);
    self.container.addSubview(vc.view)
    vc.didMoveToParentViewController(self)

This ensures that various layout and touch methods are passed through to the child VC; I suspect the layout problems you have may be due to those methods not currently being called.

Solution 2:

Tried to use the answer above but it turns out CGRectMake isn't available anymore.

Updated for Swift 3:

self.addChildViewController(vc)
vc.view.frame = CGRect(x: 0, y: 0, width: self.container.frame.size.width, height: self.container.frame.size.height)
self.container.addSubview(vc.view)
vc.didMoveToParentViewController(self)

Solution 3:

Update for Swift 4.2/5.

let listVc = ListViewController() 
listVc.view.frame = CGRect(x: 0, y: 0, width: self.listView.frame.width, height: self.listView.frame.height)
addChild(listVc)
listVc.didMove(toParent: self)

ListViewController is another view controller to be embedded.