我有这样的应用程序布局:
https://serwer1356363.home.pl/pub/appMenu.png

MainViewControler-侧面菜单中的视图(左侧带有图标的视图+来自容器视图的视图,用于显示后续视图)。

ConceptViewController-使用不同图片(图片按钮)的视图。

ConceptGalleryViewController-在这里我将要显示画廊。
画廊将显示在MainViewControler中与显示ConceptViewController相同的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:致命错误:在包装代码中的可选值时意外发现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视图。该应用程序什么都不做

最佳答案

请找到我已在情节提要中创建了容器视图并以编程方式添加子视图的示例应用程序。这对我来说可以。

源代码上传到下面的保管箱链接中。

https://www.dropbox.com/s/cz8abhltefbtqpb/ContainerDemo.zip?dl=0

09-30 14:54