I am trying to add a child view controller in code, to the current view controller from storyboard by using the next code:
UIStoryboard *storyboard = [UIStor
To create a parent-child container relationship at design time, add a container view object to your storyboard scene, as shown in image below. A container view object is a placeholder object that represents the contents of a child view controller. Use that view to size and position the child’s root view in relation to the other views in the container.
When you load a view controller with one or more container views, Interface Builder also loads the child view controllers associated with those views. The children must be instantiated at the same time as the parent so that the appropriate parent-child relationships can be created.
If you do not use Interface Builder to set up your parent-child container relationships, you must create those relationships programmatically by adding each child to the container view controller, as described in Adding a Child View Controller to Your Content.
To incorporate a child view controller into your content programmatically, create a parent-child relationship between the relevant view controllers by doing the following:
addChildViewController: method of your container view controller.
This method tells UIKit that your container view controller is now managing the view of the child view controller.Here is the code for that.
- (void)displayContentController:(UIViewController *)content {
[self addChildViewController:content];
content.view.frame = [self frameForContentController];
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self];
}
Swift:
func displayContentController(_ content: UIViewController?) {
if let content = content {
addChild(content)
}
content?.view.frame = frameForContentController()
view.addSubview(currentClientView)
content?.didMove(toParent: self)
}
More detail explanation of the same example is given at Apple developer programming guide.