我正在尝试使用以下内容并且无法添加新的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)
答案 0 :(得分:9)
您还需要在当前的控制器中添加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)