将新视图控制器的视图添加为子视图



我正在尝试以下内容,但未能添加新的视图控制器视图。这是呈现视图控制器的唯一方式吗?我们不能从其他情节提要视图控制器视图添加视图吗?

  //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)

还需要在当前控制器中添加CustomViewController作为ChildViewController

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)

最新更新