我有这样的应用程序布局: https://serwer1356363.home.pl/pub/appMenu.png
MainViewControler - 从侧面菜单中查看(左侧带图标的视图+来自containerview的视图,用于显示后续视图)。
ConceptViewController - 使用不同的图片(图片按钮)查看。
ConceptGalleryViewController - 在这里我想要显示图库。 随着ConceptViewController的显示,该库将显示在MainViewControler中的同一个containerView中。
ConceptViewController视图的MainViewControler中的ContainerView视图使用以下代码完成:
@IBOutlet weak var systemContainerView: UIView!
@IBAction func homeButtonPressed(_ sender: Any) {
showSubViewInContainerView(view: "HomeView")
}
func showSubViewInContainerView(view: String){
let controller = storyboard!.instantiateViewController(withIdentifier: view)
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
systemContainerView.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leadingAnchor.constraint(equalTo: systemContainerView.leadingAnchor),
controller.view.trailingAnchor.constraint(equalTo: systemContainerView.trailingAnchor),
controller.view.topAnchor.constraint(equalTo: systemContainerView.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: systemContainerView.bottomAnchor)
])
controller.didMove(toParentViewController: self)
}
效果很好。
当我在ConceptViewController中单击所选图标时,我使用以下代码打开ConceptGalleryViewController视图:
var idGallery = 0
@IBAction func shareTheFunBtnChecked(_ sender: Any) {
idGallery = 1
self.performSegue(withIdentifier: "toConceptGalleryView", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "toConceptGalleryView" {
let target = segue.destination as! ConceptGalleryViewController
target.idGallery = idGallery
}
}
不幸的是,它无法正常工作。 侧面菜单消失了,整个屏幕都有一块新板。
如何更改此设置以便可以在ContainerView中看到ConceptGalleryViewController?
更新
此代码不起作用:
@IBAction func shareTheFunBtnChecked(_ sender: Any) {
idGallery = 1
showSubViewInContainerView(view: "ConceptGalleryView")
}
func showSubViewInContainerView(view: String){
let controller = storyboard!.instantiateViewController(withIdentifier: view)
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "MainView") as! MainViewControler
viewController.systemContainerView.addSubview(controller.view)
NSLayoutConstraint.activate([
controller.view.leadingAnchor.constraint(equalTo: viewController.systemContainerView.leadingAnchor),
controller.view.trailingAnchor.constraint(equalTo: viewController.systemContainerView.trailingAnchor),
controller.view.topAnchor.constraint(equalTo: viewController.systemContainerView.topAnchor),
controller.view.bottomAnchor.constraint(equalTo: viewController.systemContainerView.bottomAnchor)
])
controller.didMove(toParentViewController: self)
}
我有错误:线程1:致命错误:在代码中展开Optional值时出乎意料地发现nil:
viewController.systemContainerView.addSubview(controller.view)
更新2
如果我正在尝试此代码:
let controller = storyboard!.instantiateViewController(withIdentifier: view)
addChildViewController(controller)
controller.view.translatesAutoresizingMaskIntoConstraints = false
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "MainView") as! MainViewControler
viewController.systemContainerView?.addSubview(controller.view)
controller.didMove(toParentViewController: self)
ConceptGalleryViewController视图未显示。应用程序什么都不做
答案 0 :(得分:0)
请找到我在故事板中创建容器视图并以编程方式添加子视图的示例应用程序。它对我来说很好。
源代码上传于以下保管箱链接。
https://www.dropbox.com/s/cz8abhltefbtqpb/ContainerDemo.zip?dl=0