2017-02-08 73 views
3

我嘗試使用以下內容,但未能添加新的viewcontrollers視圖。是否只能呈現視圖控制器?我們不能添加其他故事板視圖控制器視圖的視圖嗎?添加新的ViewController的視圖作爲子視圖

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

回答

7

你需要在你的電流控制器還添加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) 
+0

你錯過了另一條線 –

+0

或使用vc.willMove(toParentViewController:個體經營) –

+0

@ Anbu.Karthik編輯爲 –

相關問題