将新的Viewcontroller视图添加为子视图 [英] Adding new Viewcontroller's view as subview
本文介绍了将新的Viewcontroller视图添加为子视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下内容并且无法添加新的viewcontrollers视图。这是呈现视图控制器的唯一方法吗?我们不能从其他storyboard viewcontrollers视图添加视图吗?
I am trying with the following and failed to add new viewcontrollers view. Is it only way to present view controller ? Cant we add view from other storyboard viewcontrollers view?
//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 $ c 。$ C>在当前的控制器
You need to also add CustomViewController
as ChildViewController
in your current Controller.
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)
这篇关于将新的Viewcontroller视图添加为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文