title: How can I switch between two child View Controllers on Swift?
body: <p>I am trying to switch between two child <code>View Controllers</code> on a particular <code>Container View</code>. I have a <code>Navigation Controller</code> with a menu (<code>Table View</code> to make the different options of the menu).</p>
  
  <p>Each time I click an option of the menu I would like to change the child of the <code>Container View</code> but I am getting the child above <code>Navigation bar</code> and <code>Table View</code> (they are not shown but they are under the new child View Controller).</p>
  
  <p>The scheme of my Main.storyboard is like this:</p>
  
  <pre><code>Navigation Controller --&gt; View Controller (With 2 Container View, 1 for Table View
                                             and the other to set master View Controller)
                                           |
                                           |
                                 ------------------------
                                 |                      |
                            View Controller        Table View
                              (id: master)
  
                     View Controller (id: Home)   View Controller (id: screen2)
  </code></pre>
  
  <p>I have the following code on <code>tableView</code> function (in which I detect when an option of the menu is clicked) to change the child View Controller of the Container View:</p>
  
  <pre><code>let currentController = self.navigationController?.visibleViewController //container
  
  var oldChild = (currentController?.childViewControllers.last!)! as UIViewController //master
  let newChild = (storyboard?.instantiateViewControllerWithIdentifier("Home"))! as UIViewController //Home
  
  if (oldChild != newChild){
      if currentController.childViewControllers.last != nil{
          oldChild.willMoveToParentViewController(nil)
          currentController.navigationController?.navigationBar.willRemoveSubview(oldChild.view)
          //oldChild.view.removeFromSuperview()
          oldChild.removeFromParentViewController()
      }
  
      currentController.addChildViewController(newChild)
      currentController.view.addSubview(newChild.view)
      newChild.didMoveToParentViewController(currentController) 
  }
  </code></pre>
  
  <p>This code is working almost well. The problem is that the new child View Controller is being shown above the Navigation bar and the Table View (the menu). So it is occupying the full screen instead of fit on the Container View.</p>
  
  <p>Should I add something more to my code or am I using my code in a wrong way? I have searched a lot about it but most of the solutions are in objective-c or does not work for me.</p>
  
  <p><strong>EDIT:</strong> After searching for a lot of hours I suspect that it is something related with the embeded segue that connects the root <code>View Controller</code> with <code>master</code> View Controller but I am not able to embed the new child to the <code>Container View</code>. The code that I am trying is:</p>
  
  <pre><code>currentController.performSegueWithIdentifier("EmbedSegue", sender: newChild)
  </code></pre>
  
  <p>or </p>
  
  <pre><code>currentController.presentViewController(newChild, animated: true, completion: nil)
  </code></pre>
  
  <p>but none of them embed the segue. Just display the <code>newChild</code> on the full screen.</p>
  
  <p>Thanks in advance!</p>