将新的Viewcontroller视图添加为子视图

Gob*_*i M 4 iphone uiviewcontroller uiview ios swift3

我正在尝试使用以下内容并且无法添加新的viewcontrollers视图.这是呈现视图控制器的唯一方法吗?我们不能从其他storyboard viewcontrollers视图添加视图吗?

  //Working
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "customView") as! CustomViewController

    self.present( viewcontroller , animated: true, completion: nil)

    //Not working
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "customView") as! CustomViewController
    vc.view.frame = self.view.frame
    self.view.addSubview(vc.view)
Run Code Online (Sandbox Code Playgroud)

Nir*_*v D 9

您还需要添加CustomViewControllerChildViewController你的电流控制器.

let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "customView") as! CustomViewController
vc.view.frame = self.view.bounds
self.addChildViewController(vc)
self.view.addSubview(vc.view)
vc.didMove(toParentViewController: self) //OR  vc.willMove(toParentViewController: self)
Run Code Online (Sandbox Code Playgroud)